1. Is there really only one public class in a Java class file?
public class Test {
public static void Main (string[] args)
{
}
public class Innerclass
{
}
}
The program runs correctly, and one can conclude that a Java source file can write multiple classes, but only one of them is "public", and Java requires the source file name to be the same .
2. Classroom Exercise 2
Package class exercises;
Import Javax.swing.JOptionPane;
public class Welcome2 {
public static void Main (string[] args) {
TODO auto-generated Method Stub
Joptionpane.showmessagedialog (
NULL, "Welcome" + "\ n" + "to" + "\ n" + "Java" + "\ n" + "programming" + "\ n", "Message",
Joptionpane.plain_message);//Output dialog box
}
}
3. Why is Java specified as the Mian () method of the entry point of the program static?
In Java, the main () method is the entry method for a Java application, that is, when the program is running, the first method to execute is the main () method, which differs greatly from other methods, such as the name of the method must be main, and the method must be public static void type, the method must be a parameter that accepts a string array, and so on. Because the main () method is called by the Java Virtual machine, it must be public, and the main () method called by the virtual machine does not need to produce any objects, so the main () method is declared static and does not require a return value, so it must be declared void. The final format is as follows: public void Main (string[] args) {}.
4. Discussion of variable scopes
Variable scope: Each variable has a "valid" area (called a "scope"), out of which the variable will no longer be valid.
Package class exercises;
public class Test {
private static int value=1;
public static void Main (string[] args)
{
int value=2;
System.out.println (value);
}
}
Java variables follow the "masking principle of a variable with the same name", which is the value of the output.
A: The value of the output is the value of the valid region of the variable.type conversions in 5.Java
-No loss of precision
------> Precision Loss
| Type |
Length and description |
Range |
| Char |
16-bit, Unicode characters |
\u000~\uffff or 0~65535 |
| Byte |
8-bit, signed, byte |
-128~127 |
| Short |
16-bit, signed, short-integer |
-32768~32767 |
| Int |
32-bit, signed, integral type |
-2147483648~2147483647 |
| Long |
64-bit, signed, long Integer |
-9.223e18~9.223e18 |
| Float |
32-bit, floating-point, IEEE754 format |
About -3.4E+38~3.4E+38 |
| Double |
64-bit, floating-point, IEEE754 format |
about -1.7e+308~1.7e+30 8 |
Conclusion: The length of the large direction of small assignment, there will be a loss of precision
Hands-on brain:
public class Test {
public static void Main (string[] args)
{
System.out.println ("0.05+0.01=" + (0.05+0.01));
System.out.println ("1.0-0.42=" + (1.0-0.42));
System.out.println ("4.05*100=" + (4.05*100));
System.out.println ("123.3/100=" + (123.3/100));
}
}
Conclusion: The result of the calculation using a double type of numerical value is imprecise.
The reason ( this part comes from the network ): This involves the conversion of binary and decimal.
N-binary can be understood as: the power of the numerical x cardinality, for example, we are familiar with the decimal number 123.4=1x102+2x10+3x (10 of the 0 power) +4x (10-1 power); The same is true of the other binaries, such as binary
Number 11.01=1x2+1x (2 of 0 Power) +0+1x (2 to 2 power) = decimal 3.25.
a value of type double takes 64bit, or 64 binary numbers, except that the highest bit represents the positive and negative sign, and the lowest bit is bound to have an error with the actual data (unless the actual data is exactly 2 of the n-th square)
For example, for example, to use 4bit to represent decimal 3.26, from high to low in turn corresponds to 2 1,0,-1,-2 power, according to the top of the analysis, should be in the binary number 11.01 (corresponding to the decimal 3.25)
and 11.10 (corresponding to the decimal 3.5) select between.
In simple terms, the values we give, in most cases, require more digits than 64bit to be accurately represented (even for infinite bits), and the double type has only 64bit, after
The number of bits in the face is bound to bring an error, and the result of "mathematically accurate" cannot be obtained.
5. What is the output of the following code?
int x=100;
int y=200;
System.out.println ("x+y=" +x+y);
System.out.println (x+y+ "=x+y");
Results:
Cause: The first line of output is output x and then output y, the two do not have any add operation;
The second result is to calculate the output of the x+y first and then output "=x+y".
Course Assignment 02 (a few discussions on Java)