# Implicit Widening

j rab
Greenhorn
Posts: 16
I'm currently studying for the OCA exam and came across a question on the Enthuware.

How is this assignment valid without the F or f post fix and why would this be considered Implicit widening conversion?

Roel De Nijs
Sheriff
Posts: 10662
144
• 1
j rab wrote:How is this assignment valid without the F or f post fix and why would this be considered Implicit widening conversion?

Because any integer literal is of type long if it ends with the letter L or l; otherwise it is of type int. And an int can be implicitly widened to a float. The other way around (from float to int) will result in a compiler errorAnd it might be a bit weird, but a long can fit (implicitly converted, no explicit cast required) into a float (and a double) as well. You'll find a detailed explanation in this topic.

Please be aware that you could have precision loss if you convert a long to a doubleOutput: 9223372036854776000,000000 9223372036854775807

And for completeness: no precision loss with an int widened to a double, but widened to a float you don't get exactly the same maximum value of int. This code snippetprints: 2147483647 2147483648,000000 2147483647,000000

Hope it helps!
Kind regards,
Roel

j rab
Greenhorn
Posts: 16
Thank you Roel, that does help. So because it was an int and because a int can easily fit into an float, it was implicitly casted to a float. But if it was a floating point number than it would have been a compile time error because default floating point numbers are double. So this assignment wouldn't work:

Roel De Nijs
Sheriff
Posts: 10662
144
• 2
j rab wrote:So because it was an int and that a int can easily fit into an float it was implicitly casted to float. But if it was a floating point number than it would have been a compile time error because default floating point numbers are double.

Exactly!

This statement will indeed fail to compile due to the fact 0.23 is by default considered to be a double literal and you can't assign a double to a floatIn order to get rid of the compiler error, you have two alternative solutions; either use F or f (and create a float literal instead of a double literal) or add an explicit cast to float (or an integer data type like int or short, might of course cause precision loss). This cde snippet illustrates both alternatives

Hope it helps!
Kind regards,
Roel