Example 1: Output of a char array
1System.out.println ("H" + "a");//Output: Ha2System.out.println (' H ' + ' a ');//Output: 1693System.out.println ("" + ' H ' + ' a ');//Output: Ha4System.out.println ("//////////////////");5System.out.println ("2 + 2 =" + 2 + 2);//Output: 2 + 2 =6System.out.println ("2 + 2 =" + (2 + 2));//Output: 2 + 2 = 47System.out.println ("//////////////////");8 Char[] char1 = {' 1 ', ' 2 ', ' 3 '};9Object char2 =New Char[] {' 1 ', ' 2 ', ' 3 '};TenSystem.out.println (CHAR1);//output: 123 OneSystem.out.println (CHAR2);//output: [[email protected] ASystem.out.println ("test-" + char1);//output: Test-[[email protected] -System.out.println ("test-" + char1.tostring ());//output: Test-[[email protected] -System.out.println ("test-" + string.valueof (char1));//Output: test-123 theSystem.out.println ("//////////////////");
Example 2:
1Random random =NewRandom ();2StringBuffer Sbuffer =NULL;3 Switch(Random.nextint (2)) {4 Case1:sbuffer =NewStringBuffer (' A ');5 Case2:sbuffer =NewStringBuffer (' B ');6 default: Sbuffer =NewStringBuffer (' C ');7 }8Sbuffer.append (' a ');9Sbuffer.append (' B ');TenSbuffer.append (' C '); OneSystem.out.println (Sbuffer);//output: ABC A}
This procedure has three bugs:
- Error in using the random.nextint (int n) Method:
First look at the API for this method:
public int Nextint (int n)
Returns a pseudo-random number, which is an int value that is evenly distributed between 0 (inclusive) and the specified value (not included) from this random number generator sequence.
Therefore, this should be changed to: Switch (Random.nextint (3)) {
- There is no break statement in the case clause:
Causes the preceding assignment to be overwritten by subsequent assignments.
- The construction method of the StringBuffer class uses an error:
StringBuffer does not support the constructor of an incoming parameter character (char) and should be passed in as a string,
Therefore, should read: Sbuffer = new StringBuffer ("A");
Example 3: The code that will die loop
1 //dead Loop 12 intStart1 = integer.max_value-1;3 for(intStart7=start1; start7<=start1+1; start7++) {4System.out.println ("loop.."));5 }6 //dead Loop 27 DoubleStart2 = 1.0/0.0;8 DoubleStart3 = double.positive_infinity;//equivalent to the above sentence, can replace9 while(Start2 = = (start2+1)) {TenSystem.out.println ("loop..")); One } A //dead Loop 3 - DoubleSTART4 = 0.0/0.0; - DoubleStart5 = Double.NaN;//equivalent to the above sentence, can replace the while(Start4! =start4) { -System.out.println ("loop..")); - } - //dead Loop 4 +String start6 = "Test"; - while(Start6! = (start6+0)) { +System.out.println ("loop..")); A } at //dead Loop 5 - byteStart7 =-1;//dead Loop - //Short start7 =-1;//dead Loop - //int start7 =-1;//Cycle 32 times - //Long start7 =-1;//Cycle 64 times - intCount = 0;//Number of Cycles in while(Start7! = 0) { -Start7 >>>= 1; toSystem.out.println ("loop..")); +count++; - } theSystem.out.println ("Number of cycles =" +count); * //dead Loop 6 $ intSTART8 =Integer.min_value;Panax Notoginseng //Long start8 = long.min_value; - while(Start8! = 0 && Start8 = =-start8) { theSystem.out.println ("loop..")); +}
Additional notes:
- About Infinity and Nan (Dead Loops 2 and 3)
1 DoubleD = 1.0/0; 2System.out.println (d);//Output: Infinity3System.out.println (d + 1);//Output: Infinity4System.out.println (d = = d + 1);//Output: True5 6d = 0.0/0; 7System.out.println (d);//Output: NaN8System.out.println (d + 1);//Output: NaN9System.out.println (d = = d + 1);//Output: FalseTen OneSystem.out.println (Double.NaN = = Double.NaN);//Output: False ADouble A =NewDouble (Double.NaN); -Double B =NewDouble (Double.NaN); -System.out.println (A.equals (b));//Output: True
- Automatic narrowing native type conversion for Java (Dead Loop 5)
Analysis of short start7 =-1; and Start7 >>>= 1; These two lines of code:
When performing a shift operation, the first step is to promote start7 to the START7NT type, because all arithmetic operations perform such an increase on the operands of the Short,byte,char type;
This promotion is to broaden the native type, no loss of information, the execution of the symbol extension, the value of Start7 after the 0xffffffff,start7>>>1 is the 0x7fffffff;
When this value is re-deposited in start7, in order to put int value into the short variable, Java automatically executes the dreaded narrow native type, directly the high 16 bits off, the remaining (short) 0xFFFF, and back to the starting point, resulting in a dead loop.
This reminds us not to use the compound assignment operator on the Short,byte,char type, which is prone to problems, and this implicit narrowing type conversion, while losing information, can also be catastrophic.
- About special values in Java (Dead loop 6)
Java uses the 2 complement arithmetic operation to be asymmetrical.
For each signed integer type (byte, short, int, and long), the negative value is always one more than the positive value, and the extra value is always the smallest value that the type can represent;
Negative values on Integer.min_value do not change its value, negative values will overflow, but Java automatically ignores this overflow, as well as long.min_value.
"Java Doubts" book excerpt