I wrote this code:
float b = 3.6;
and I get the following:
Error: Unresolved compilation problem: Type mismatch: cannot convert from double to float
Why? What is the definition of float ?
float
In Java, when you enter a decimal number as 3.6 , it is interpreted as double . double is the 64-bit precision of the IEEE 754 floating point, and float is the 32-bit precision of the IEEE 754 floating point. Since a float less accurate than a double , the conversion cannot be performed implicitly.
3.6
double
If you want to create a float, you must end your number with f (i.e.: 3.6f ).
f
3.6f
See the Java Tutorial Primitive Data Type Definition for more details.
Do this
float b= 3.6f;
A floating-point literal is of type float if the letter ASCII F or f is added to it; otherwise, its type is double and you can add the suffix of the ASCII letter D or d to it.
The fact is that decimal numbers double by default. And since double does not fit into a float, you should directly say that you intentionally define a float. So go with:
float b = 3.6f;
In JAVA, values ββsuch as:
It is assumed that double , not float .
You can also do a roll to solve the problem:
float b = (float) 3.5 ;
float b = (float) 3.5
Another solution:
float b = 3.5f ;
float b = 3.5f