Conversion of Java Basic Data Types

Source: Internet
Author: User
Type conversion

Java is a strongly typed language. Strong languages have the following requirements:

A variable or constant must have a type: the type must be declared when the variable or constant is declared and can only be used after the declaration.

The value type must be the same: the value type must be the same as the type of the variable or constant.

The operation type must be consistent: the data types involved in the operation must be consistent before the operation.

However, in actual use, operations are often performed between different types of values, which requires a new syntax to adapt to this need. This syntax is data type conversion.
In numerical processing, the logic of the computer and the reality is not the same. For reality, 1 and 1.0 are no different, but for the computer, 1 is an integer, while 1.0 is of the decimal type, and its storage method and occupied space in the memory are different. Therefore, type conversion is necessary within the computer.

There are two types of data conversion in Java:

Automatic type conversion: the compiler automatically converts data types and does not needProgramWriteCode.

Forced type conversion: forces the compiler to perform type conversion. You must write code in the program.

Because the boolean type is not a numeric type in the basic data type, the conversion of the basic data type is between seven other types except the boolean type. The following describes the rules for conversion of the two types, the applicable scenarios, and the issues that need to be paid attention to during use.

Automatic type conversion

Automatic type conversion, also known as implicit type conversion, refers to the type conversion that is automatically completed by the system without writing code. In actual development, many such types are converted. Therefore, the Java language does not design the syntax for this operation, but is automatically completed by JVM.

Conversion rules: from a small storage range to a large storage range.
Specific rules: byte → short (char) → int → long → float → double

That is to say, the byte type variables can be automatically converted to the short type. Example code:

 
ByteB = 10;

Short SH = B; here, when assigning values, JVM first converts the value of B to the short type, and then assigns the value to sh.
You can skip during type conversion. Sample Code:

 
ByteB1 = 100;IntN = b1;

Note: During type conversion between integers, the numeric value does not change. However, when the integer type, especially the larger Integer type, is converted to the decimal type, the storage method is different, there may be loss of data precision.

 

Forced type conversion

Forced type conversion, also known as explicit type conversion, refers to the type conversion that must be completed by writing code. This type conversion may result in loss of precision. Therefore, you must write the corresponding code and be able to tolerate this loss before performing this type conversion.

Conversion rules: from a large storage range to a small storage range.

Specific rules: Double → float → long → int → short (char) → byte

Syntax format: (converted to type) value to be converted

Sample Code:

 
DoubleD = 3.10;IntN = (Int) D;

Here, the variable D of the double type is forcibly converted to the int type, and then assigned to the variable n. It must be noted that the decimal place is forcibly converted to an integer, And the decimal place is discarded unconditionally. The above result is 3. When an integer is forcibly converted to an integer, the low position of the number is taken. For example, if an int type variable is converted to a byte type, only the low 8 bits (that is, the last byte) of the int type are removed).
Sample Code:

 
IntN = 123;ByteB = (Byte) N;IntM = 1234;ByteB1 = (Byte) M;

the value of B is still 123, and the value of B1 is-46. The Calculation Method of B1 is as follows: the value of M is converted to binary 10011010010, and the low 8-bit value of this number is taken as the value of B1, then the binary value of B1 is 11010010, according to the number of machines, the highest bit is the symbol bit. 1 represents a negative number. If a negative number is stored in a computer as a complement code, the original code of the negative number is 10101110, and the value is-46 in decimal format.
Note: Forced type conversion usually results in loss of precision, so be cautious when using it.

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.