Data type conversion based on Java language

I. Overview

Data type conversion is the process of changing a value from one type to another.
If you convert from a low-precision data type to a high-precision data type, you will never overflow and always succeed; while converting a high-precision data type to a low-precision data type may cause information loss and may fail.
There are two ways of data type conversion, namely implicit conversion and explicit conversion .

Two, implicit type conversion

The conversion from a low-level type to a high-level type will be performed automatically by the system, and the programmer does not need to perform any operations. This type of conversion is called implicit conversion. The following basic data types involve data conversion, excluding logical types and character types. The order of these types from lowest to highest precision is: byte<short<int<long<float<double .

Three, explicit type conversion

When assigning high-precision variable data to low-precision variables, you must use explicit type conversion operations (also known as forced type conversion).
The syntax format is as follows:
(type name) the value to be converted
Common examples of explicit data type conversion:
int a = (int)45.6; //double to int, the result is 45
long b = (long)456.6F; // float to long, the result is 456
int c = (int)'d'; //char to int, the result is 100

Explanation: When assigning an integer literal value to a byte, short, int, long variable, if the literal If the value of the quantity does not exceed the value range of these variables, it can be compiled and run without the need to strengthen the type conversion symbol. Otherwise, the type conversion symbol must be strengthened for forced type conversion.
For example:
byte b = 127;
the value range of byte is -128~127, although it is converted from int type to byte type, since 127 does not exceed the value range of byte, it can be forced without adding (byte) Conversion. The complete wording should be:
byte b = (byte)127;
byte b = (byte)128; //Forced type conversion, loss of precision, the result is -128

4. Summary of main points

1. Among the eight basic data types, except for the boolean type that cannot be converted, the remaining seven types can be converted;
2. If the integer literal does not exceed the value range of byte, short, and char, you can directly change it Assign values ​​to variables of type byte, short, and char;
3. The conversion from small capacity to large capacity is called automatic type conversion. The order of capacity from small to large is: byte<short/char<int<long<float<double , where short and Char occupies two bytes of capacity, but char can represent a larger positive integer;
4. The conversion from large capacity to small capacity is called forced type conversion. When writing, you must add a "compulsory type conversion symbol", but precision may appear Loss;
5. When the byte, short, and char types are mixed, they are converted to int type before the operation;
6. When multiple data types are mixed, each is converted to the one with the largest capacity before the operation.


The above content is a bit of personal learning experience. If there are errors in the knowledge points, please leave a message to remind you, if there is any infringement content reminder, delete it immediately.

Guess you like

Origin blog.csdn.net/pf6668/article/details/107287840