Conversion of Java Basic Data Types

Source: Internet
Author: User

● The boolean type cannot be converted to another data type;

● Integer, complex, and floating point data are converted to each other in hybrid operations. The conversion rules are as follows:

■ Small data types are automatically converted to large data types. data types are sorted by capacity as follows: byte, short, char à int à long à float à double

Note that byte, short, and char are not converted to each other,The three of them are first converted to the int type during calculation. The value range of byte is-128 ~ 127

When converting a data type with a large capacity to a data type with a small capacity, a forced conversion character must be added, but it may cause precision.

Decrease or overflow.

N when there are multiple types of data hybrid operations, the system first automatically converts all types to the one with the largest capacity

Data Type before calculation.

● The default value of a real number constant (for example, 1.2) is double. (So: float f = 22.2; it is incorrect)

● The default Integer type (for example, 123) is int.

You can directly assign a value of the int type to a byte, short, and char type variable, but cannot exceed the corresponding range (directly extract the last byte ).

It should be understood that the essence of forcibly converting a large number into a small number is to intercept the following bytes (for example, when the int type is converted to the double type, is to directly take the last four bytes of the int type), but the double type is converted to the float type, because there is a decimal point in the double, therefore, infinity cannot be converted when the conversion is directly enhanced ).

8 bytes of the long type, 4 bytes of the int type, 2 bytes of the short type, and 1 byte type.

Float Type: 4 bytes; double type: 8 bytes.

L typical problems:

(1) double d = 1e200; // print the display Infinity;

(2) float f = 11.1 // error; double type by default, should be changed to float f = 11.1f;


) Public class TestDataKind {public static void main (String [] args) {byte B = 44; char c = 'B'; short s = 1024; int I = 40000; long l = 12463l; float f = 35.67f; double d = 3.1234d;/* (f * B), when B is automatically upgraded to the float type (l * f, l when c is automatically upgraded to the float Type (I/c) * and c is automatically upgraded to the int type * (d * s), s is automatically upgraded to the double type and then added, the intermediate results are changed to the double type. * Here, result can only be declared as double type. If result is declared as another type, an error will occur unless * forced type conversion */double result = (f * B) + (l * f) + (I/c)-(d * s); System. out. print (f * B) + "+" + (l * f) + "+" + (I/c) + "-" + (d * s )); system. out. println ("=" + result );}}

(4) byte b1 = (byte) 128;

Byte b2 = (byte)-129;

System. out. println (b1 );

System. out. println (b2 );

Printed results:-128 and 127

Analysis: byte only has eight bits and can only represent-128-127. The negative number in the computer is stored as a supplementary code. Returns the inverse of the binary value of the absolute value of a negative number. For example, if the binary code of the absolute value of-1 is 00000001, the result is 11111110 after the result is obtained. -The binary code of the absolute value of 128 of 128 is 10000000, And the inverse result is 01111111, plus 1 to 10000000, which is the maximum representation range of bytes. The binary code of the absolute value of-129 129 is 10000001. The result is 01111110 after the inverse value is 01111111, and 127 after the first value is added. The final result is.

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: 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.