The following works
float a=3;
but the following doesn’t:
Float a=3;
Shouldn’t 3 be automatically promoted to float (as widening conversions don’t require an explicit cast) and then Boxed to Float type ?
Is it because of a rule I read in Khalid Mogul’s Java book ?
Widening conversions can’t be followed
by any boxing conversions
The reason why
Float a=3;won’t work is because the compiler wraps the3into it’s Integer object (in essence, the compiler does this:Float a = new Integer(3);and that’s already a compiler error). Float object isn’t and Integer object (even though they come from the sameNumberobject).The following works:
which in essence is translated by the compiler as:
or as Joachim Sauer mentioned,
Hope this helps.