Data type conversion issues in Java

Source: Internet
Author: User

The Boolean type cannot be converted to his data type.

The precedence of byte (8-bit), short (16-bit), char three types in Java is the same, and the same priority is not automatic (if you convert to one, you must force type conversions ), only low types are automatically converted to high types, for example, You can convert a byte to an int type automatically. (char is 2 bytes in Java.) Java uses unicode,2 bytes (16 bits) to represent one character. When you assign data of type char to a high-priority type, the type conversion is automatic and the Unicode encoding of the char is assigned to a variable of the high-priority type.

Integer, character, and floating-point data are converted to each other in a mixed operation, which follows the following principles:

Types with small capacity are automatically converted to large data types; The data types are sorted by capacity size:

1, Byte,short,char-->int (32-bit) - Long (64-bit) - Float (32-bit) - double (64-bit)

Byte,short,char do not convert to each other, they are mixed with other data types when the calculation is first converted to int type

2, the large capacity of the data type converted to a small capacity data type, to add a forced conversion, but may result in reduced precision or overflow;

3, there are many types of data blending operations, the system first automatically converts all data to the largest capacity of the data type, and then the calculation.

4, Real constants (such as: 1.2) default to Double.

5, integer constants (such as: 123) default to int.

# # # #值得注意的是:

the equivalence between values is essentially compared to whether their binary storage is equal. For example: 0.3f==0.3 returns False (32-bit 2-binary 0.3 representation and 64-bit 0.3 representation is different); but 0.5f==0.5 returns True (because the binary representation of 32 and 64 bits is the same)

Data type conversion issues in Java

Related Article

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.