Reputation: 43
float firstFloatValue = 5;
float secondFloatValue = 2.5;
Why is that the first variable without an f at the end doesn't give an error while the second variable does? I know that if we don't put an f at the end of the literal, it is assumed as a double. But why there is not an error while initializing the first variable by 5 without an f at the end. Please help me, I am very new to Java.
Error message:
Main.java:12: error: incompatible types: possible lossy conversion from double to float
float secondFloatValue = 2.5;
Upvotes: 0
Views: 38
Reputation: 436
Because of type casting firstFloatValue example int is type casted to float ( small memory data types into higher memory data types ) default behaviour. But in second double cant be type casted to float
Upvotes: 0
Reputation: 86774
You didn't include the error message (ALWAYS include the error message on StackOverflow).
Without the type suffix f
(as in 2.5f
) the literal 2.5
is interpreted as a double
, which does not fit in a float
variable.
Upvotes: 1