Java Learning Note 5--type conversion

Source: Internet
Author: User

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

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.