A forced conversion of data base types, an understanding of automatic conversion

Source: Internet
Author: User

When you encounter data transformations, it is always easy to ignore the conversion between the data and whether you can get the correct results. The summary is as follows: 1. Automatic promotion to large range in small scope

Different types of data are generally automatically promoted in small areas toward a wide range of directions. It can be understood that a large range of precision is larger than a small range, or the number of digits is more.

such as Byte b=3; B=b+2 will complain because 2 is of type int. Cannot force down to byte type.

and Byte b=3; int a=b+1; The result a=4 is obtained because the B is automatically promoted to the int type; 2. Large scale to small range casts

To revert back to the byte type, a cast is required.

such as Byte b=3;  B= (byte) b+1; Of course, if the results overflow must be an error. 3. Automatic conversion (more interesting and characteristic, also relatively easy to overlook)

Mentioned earlier, short s=5; S=s+3 is a wrong type of report. And s+=3 will get the result s=8;

This is because the computer sees S+=3 as s= (the type of data on the left) s+3. And get the right results.

This example is usually assigned to the right.

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.