Java Basic data types
| Java Basic data types |
| Serial number |
Type |
Default value |
Size/bit |
| 1 |
Byte |
(byte) 0 |
8 |
| 2 |
Short |
(short) 0 |
16 |
| 3 |
Int |
0 |
32 |
| 4 |
Long |
0L |
64 |
| 5 |
Float |
0.0f |
32 |
| 6 |
Double |
0.0d |
64 |
| 7 |
Char |
\u0000 |
16 |
| 8 |
Boolean |
False
|
1 |
Conversion of basic data types
Automatic conversion
In general, a variable of a data type is defined and can no longer be converted arbitrarily. However, Java allows the user to make a limited type conversion to the base data type.
Java will do the type conversion automatically if the following conditions are met:
(1) Conversion from "small" data to "big" data.
It is obvious that the numeric representation range of the "small" data type is less than the numeric representation range of the "large" data type, i.e. the precision is less than the "large" data type.
Therefore, if the "big" data is converted to "small" data, data precision is lost. For example: Long to int, data that exceeds the range of int will be lost, leading to uncertainty about the result.
Conversely, the "small" data to "big" data conversion, there will be no data loss situation. For this reason, this type conversion is also known as widening conversion .
These types are from "small" to "large" respectively: (byte, Short,Char) <int<Long<float<Double。
What we call "big" and "small" here is not the amount of bytes, but the size of the range that represents the value.
(2) The data types before and after conversion are compatible. Because the Boolean type can only hold true or FALSE, which is incompatible with integers or characters, type conversions are not possible.
(3) When the integer type and floating-point type are evaluated, the result is converted to a floating-point type.
eg
Longx = 30;
floaty = 14.3f;
SYSTEM.OUT.PRINTLN ("x/y =" + x/y);
Output:
x/y = 1.9607843
Visible long Although the precision is greater than the float type, the result is a floating-point number type.
Forced Conversions
You can make a forced conversion of the data type when it does not conform to the auto-transition criteria or according to the user's needs.
The conversion method is as follows:
floatf = 25.5f;
intx = (int) F;
System.out.println ("x =" + x);
Java Basic data types