Java Learning Notes

Source: Internet
Author: User
Tags arithmetic operators instance method

Question 1:

Is there really only one public class in a Java class file?

New Test01.java, enter the following code

1  Public classTest012 {3      Public Static voidMain (String args[])4     {5         6     }7 8      Public classInnerclass9     {Ten      One     } A}

Compile

Compilation succeeded, resulting in two. class files

If the internal class name is also set to TEST01, the compilation does not pass

You can say this:

Because an inner class exists as a member of an external class, members of the class can be decorated with public

In a. Java source file, there can be at most one public class with the same name as the source file

2. Challenge 2:

Fixed format for Main method: public static void Main (String args[])

Can I change the return value of the main () method from void to int?

Practice is the truth

New Test02.java, enter the following code

1  Public class Test02 2 {3      Public Static int Main (String args[]) 4     {5         return 0;     6     }7 }

change void to int and add the corresponding return value

Compile, compile, pass

Try again to run, the result is error

Void indicates that the main method has no return value, because Java does not require the Main method to return exit information to the operating system. If the main method exits normally, the Java application's exit code is 0, indicating that the program was successfully run.

In addition, the other parts of the main method can not be changed, the only change is string[] args may be written as String args[], and the name of the args can be changed

3. Think:

What is a static method?

A method that you can use without creating an object.

Why is the Java rule static as the main () method of the entry point of the program?

A: Because the Main method is static, the JVM calls this method without having to create any instances that contain the main method.

If the main method is not declared static, the JVM must create an instance of the main class, because the constructor can be overloaded, and the JVM cannot determine which main method to invoke.

static methods and static data are loaded into memory and can be called directly without the need to create an instance like an instance method, and if the main method is static, it will be loaded into the JVM context as an executable method

4. Each variable has a "valid" area (called a "scope"), out of which the variable will no longer be valid.

Take a look at the sample code below, what is the output?

1  Public classTest032 {3     Private Static intValue=1;4     5      Public Static voidMain (String args[])6     {7         intvalue=2;8 System.out.println (value);9     }Ten}

Output results

Java variables Follow the masking principle of the variable with the same name

Each variable has a "valid" area (called a "scope"), out of which the variable is no longer valid, and the variable with the same name has the principle of automatic masking within the specified range. That is, local variables can have the same name as global variables, and when referenced within a function, local variables of the same name are used instead of global variables.

Type conversions in 5.Java

Run the following code

1  Public classTest042 3 {4 5  Public Static voidMain (String args[])6 7 {8 9System.out.println ("0.05 + 0.01 =" + (0.05 + 0.01));Ten  OneSystem.out.println ("1.0-0.42 =" + (1.0-0.42)); A  -SYSTEM.OUT.PRINTLN ("4.015 * 100 =" + (4.015 * 100)); -  theSystem.out.println ("123.3/100 =" + (123.3/100)); -  -     } -}

Run results

Accuracy loss Reason:

float and double types are primarily designed for scientific calculations and engineering calculations, and they perform binary floating-point operations, which are divided into three parts: The sign bit, the exponent, and the tail part of the computer.

When data is stored as binary in the computer, a series of wireless loops appears, and errors occur during data conversion. Decimal cannot accurately represent 1/3, the same binary cannot accurately represent 1/10, and most of the floating-point numbers are stored in the computer as an approximation.

Java in the decimal and binary conversion problems, double type of the value of 64bit, that is, 64 binary number, except the highest bit represents the positive and negative sign of the bit, in the lowest bit will be with the actual data error, simply is we give the value, in In most cases it takes more than 64bit of digits to be accurately represented (even in the case of an infinite number of bits), whereas a double type has a value of only 64bit, and the number of digits behind it will definitely bring an error, and the result of "mathematically accurate" cannot be obtained.

6 How to handle the loss of precision?

  Workaround-Use the BigDecimal class

1 ImportJava.math.BigDecimal;2 3  Public classTestbigdecimal4 {5      Public Static voidMain (string[] args)6     {7BigDecimal F1 =NewBigDecimal ("0.05");8BigDecimal F2 = bigdecimal.valueof (0.01);9BigDecimal F3 =NewBigDecimal (0.05);TenSystem.out.println ("Use string as the calculation result of the BigDecimal constructor parameter:"); OneSystem.out.println ("0.05 + 0.01 =" +F1.add (F2)); ASystem.out.println ("0.05-0.01 =" +f1.subtract (F2)); -SYSTEM.OUT.PRINTLN ("0.05 * 0.01 =" +f1.multiply (F2)); -System.out.println ("0.05/0.01 =" +f1.divide (F2)); theSYSTEM.OUT.PRINTLN ("Use double as the calculation result of the BigDecimal constructor parameter:"); -System.out.println ("0.05 + 0.01 =" +F3.add (F2)); -System.out.println ("0.05-0.01 =" +f3.subtract (F2)); -SYSTEM.OUT.PRINTLN ("0.05 * 0.01 =" +f3.multiply (F2)); +System.out.println ("0.05/0.01 =" +f3.divide (F2)); -     } +}


Operation Result:

You should use a string instead of a double value when building a BigDecimal object, or it is still possible to raise the problem of computational precision. (why is this?) )

Double+,-cannot accurately represent BigDecimal (String) 16-bit effective numbers when using bigdecimal, it makes sense to apply the *,/constructor to create objects. An object created by BigDecimal cannot use traditional arithmetic operators to perform mathematical operations on its objects directly, but must call the corresponding method. The argument in the method must be an BigDecimal object.

7. what is the output of the following code?

1         int x=100; 2         int y=200; 3         System.out.println ("x+y=" +x+Y); 4         System.out.println (x+y+ "=x+y");

Operation Result:

Reason:

Because double quotation marks are type string, String+int type is auto-converted, int to string,100 is automatically converted to string type, for two strings, + performs a string connection operation, if output, the result is x+y=100, then proceed, 200 is converted to a string type, so the output is x+y=100200

From left to right, the first is 100+200, two variables are int type, so the calculation, at this time is the mathematical addition, the int type of 300, then the int type of 300 and the string type of "=x+y" to do the operation, int type automatically converted to string, and then + Perform a string join operation to get 300=x+y

Java Learning Notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.