This is my code:
class test{
public static void main(String arg[]){
int a=10, b=20;
float c = 30.123;
int e = (int)c;
System.out.println(e);
}
}
I'm getting this error:
test.java:6: error: possible loss of precision
float c = 30.123;
^
required: float
found: double
1 error
Why all these?
Floating point literals are by default of double
type. And assigning a double value to a float type will result in some precision error. You can either change the type
of c
to double
, or append an f
at the end of it to make it float
like so:
float c = 30.123f;
If you specify float value without f at the end it is treated as double
which is by-default.
double d = 30.123;
For float literal you should append f at the end of float value.
float c = 30.123f;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.