Java Basic data type conversions

Source: Internet
Author: User

Type Conversions

The Java language is a strongly typed language. There are several requirements for strongly typed languages:

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

The type must be identical when assigning: the type of the value must be exactly the same as the type of the variable or constant.

The operation must be of the same type: The data type of the participating operation must be consistent to operate.


However, in practical use, it is often necessary to operate between different types of values, which requires a new syntax to accommodate this need, which is the conversion of data types.
In the numerical processing this part, the computer and the reality logic is not quite the same, for the reality, 1 and 1.0 makes no difference, but for the computer, 1 is the integer type, and 1.0 is the decimal type, its storage in memory and occupies the same space, so the type conversion inside the computer is necessary.

There are two types of data type conversions in the Java language:

Automatic type conversion: The compiler automates the type conversion and does not need to write code in the program.

Coercion type conversions: Forces the compiler to type-convert and must write code in the program.

Because the Boolean type in the base data type is not a numeric type, the conversion of the base data type is a conversion between 7 types other than the Boolean type. Here are the rules for two types of conversions, where to use them, and the issues to be aware of when using them.


Automatic type conversion

Automatic type conversion, also known as implicit type conversion, refers to a type conversion that is done automatically by the system without writing code. Because of the many types of conversions in real-world development, the Java language was designed so that it did not design the syntax for the operation, but was done automatically by the JVM.


Conversion rules: From a type with a small storage scope to a type with a large storage range.
The specific rules are: Byte→short (char) →int→long→float→double

This means that a variable of type Byte can be automatically converted to the short type, the sample code:

byte B = 10;

Short sh = b; In this assignment, the JVM first converts the value of B to the short type and then assigns the value to sh.
You can jump on type conversions. Example code:

BYTE B1 = 100;int n = b1;

When declaring and defining variables, integers are by default the type int, and decimal is the double type by default. If you want to declare that the decimal is a float type, you must follow the number F or F after the decimal. To declare an integer of type long, you must follow the integer followed by L or L.

Note: When converting between integers, numeric values do not change, and when converting integer types, especially large integer types, to decimal types, there may be a loss of data precision due to different storage methods.

Forcing type conversions

Coercion type conversions, also known as explicit type conversions, are type conversions that must be written in order to complete the code. This class type conversion is likely to have a loss of precision, so you must write the appropriate code and be able to tolerate that type of conversion.

Conversion rules: From a type with a large storage range to a type with a small storage range.

The specific rules are: Double→float→long→int→short (char) →byte

The syntax format is: (the type converted to) the value that needs to be converted

Example code:

Double D = 3.10;int n = (int) d;

Here, the variable d of type Double is cast to the int type, and then the variable n is assigned. It should be explained that the decimal is cast to an integer, using the "Go 1 Method", that is, unconditionally discard all the digits of the decimal point, then the result of the conversion is 3. When an integer is cast to an integer when the low of the number is taken, for example when a variable of type int is converted to a byte type, only the value of the lower 8 bits (that is, the last byte) of the int type is gone.
Example code:

int n = 123;byte b = (byte) n;int m = 1234;byte B1 = (byte) m;

The value of B is still 123, and the value of B1 is-46. B1 is calculated as follows: The value of M is converted to binary is 10011010010, take the number low 8 bit value as the value of B1, then the B1 binary value is 11010010, according to the machine number, the highest bit is the sign bit, 1 is negative, in the computer negative numbers are stored in the complement, The original code for the negative number is 10101110, which is the decimal-46.
Note: Coercion of type conversions usually loses precision , so use caution


Java Basic data type conversions

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.