Today Bi Xiangdong Teacher explained the type conversion, its use seems not very wide, but the feeling is very interesting, estimated a lot of students after learning a little dizzy, so the teacher explained several points, summed up as the following code, I believe that through comparison, can have a more thorough understanding of the tutorial
classtypeconversion{ Public Static voidMain (string[] agrs) {byteb = 2;//since 2 is a constant, the system will automatically determine whether 2 is within the Bate range, so it can be assigned to B, and the next sentence in the right of the B is a variable, the system can not be judged, and therefore automatically promoted to int type//b=b+3;//Automatic conversion: Here will be an error "loss of precision", because 3 defaults to int, so the right side of the equation B is automatically converted to int, 4 bytes, cannot be assigned to the left of the equation 2 bytes of bB= (byte) (b+3);//Cast: Cast the result of the b+3 to a byte typeSystem.out.println (b);//Run Result: 5System.out.println (' a ');//operation Result: aSystem.out.println (' a ' + 1);//running Result: 98System.out.println (' A ' + 1);//Operation Result:SYSTEM.OUT.PRINTLN (1);//Run Result: 1System.out.println (' 1 ');//Run Result: 1System.out.println (1+0);//Run Result: 1System.out.println (' 1 ' +0);//Operation Result:System.out.println ((Char) 1);//running result: Human faceSystem.out.println ((Char) ' 1 ');//Run Result: 1System.out.println ((Char) (' a ' + 1));//Run Result: b//the ASCII value of character 1 corresponds to//in the code above you can see that the character 1 and the number 1 have a fundamental difference }}
Java Learning Note 5--type conversion