Read the basics of the javaSE language over the past two days and record some issues that have not been paid too much attention.
Question 1:
Package com. ccit. yichu;
Public class LightSpeed {
/**
* This instance mainly tests integer variable overflow.
* This program calculates the number of kilometers taken by light for 1000 days.
* Changing the second and distance types to int in this program will not return an error, but the result is-127631360 because:
* For class member variables, the default initial value of integer variables is 0, but the local variables declared in the method do not have the default value,
* The program is not initialized during compilation, so there is no overflow problem,
* When the program is running, the variables second and distance are of the int type and cannot represent 25920000000000. overflow occurs, so the result is incorrect.
* @ Param args
*/
Public static void main (String [] args ){
Int speed = 300000;
Int days = 1000;
Long second;
Long distance;
Second = days * 24*60*60*365;
Distance = speed * second;
System. out. println ("1000 days by light" + distance + "km ");
}
}
Question 2:
Package com. ccit. bijiao;
Public class Denghao {
/**
* This program mainly verifies whether the "=" and "equals ()" functions are equivalent for string comparison.
* The result is not equivalent.
* Note: String is not a basic type, but a reference type. Therefore, two strings are equal (that is, "="), which means the two references point to the same space.
* Although e and f are of the same length, type, and value, the calculation result is still false.
* E. equals (f) means that e and f are of the same length, type, and value, but not necessarily pointed to the same space.
* @ Param args
*/
Public static void main (String [] args ){
String a = "aaa ";
String B = "aaa ";
String c, d;
C = "aaa ";
D = "aaa ";
String e = new String ("aaa ");
String f = new String ("aaa ");
System. out. println ("a = B ----------" + (a = B ));
System. out. println ("a. equals (B) ----------" + a. equals (B ));
System. out. println ("c = d ----------" + (c = d ));
System. out. println ("c. equals (d) ----------" + c. equals (d ));
System. out. println ("e = f ----------" + (e = f ));
System. out. println ("e. equals (f) ----------" + e. equals (f ));
}
}
Question 3:
Public class Test {
/** I/O knowledge is required to test how to output the result to a text file.
* In the textbook, it means to run javac in the doscommand to compile the java file.
* Then run java to run the Test. class file,
* Format: java Test> F: \ result.txt
* You can output the running result of this program to F: \ result.txt.
* Note: This file can run successfully only when it is in the default package,
* The Yest file can only be compiled successfully but cannot be run,
* The exception of the class cannot be found.
* But why ?!!
*
* @ Param args
*/
Public static void main (String [] args ){
String a = "hello world! ";
System. out. printf ();
}
}