Decimals in Java are called floating-point numbers
1. There are two types of:
FLOAT: single-precision floating-point number. 4 bytes.
Double: two-precision floating-point number. 8 bytes.
2. Type conversion
Small capacity -------------------------------> Large capacity
Byte,short,char << int << long << float << double
The small size of the type will automatically increase the capacity of large, while the large capacity of the small turn, the loss of precision occurs.
as follows, when the integer 127 is assigned to a variable of float and double, the type is automatically promoted and the output is a floating-point number.
Public class MyTest { publicstaticvoid main (string[] args) { float a=127; double b=127; System.out.println (a); System.out.println (b); }}
Output Result:
127.0
127.0
3. By default, floating-point number is double type
The following: 127.0 is a double type, so the variable is assigned to the float type, the type is reduced, the accuracy is lost, and an error is given.
Public class MyTest { publicstaticvoid main (string[] args) { float a=127.0; System.out.println (a); }}
If you want to assign a value of 127.0 to a
Can be forced type conversion, written as float a=127.0f;
Decimals in Java