the above code compiles fine... but I've got a question.. Floating point literals are defined as double, so my first guess was that this code didn't compile because it tried to fit a double value into a float. Now, if modify line 1:
it raises the compiler error I was expecting. so, is line 1 of the initial code treated as an int? not only for line 1 but for all other lines? [ June 23, 2003: Message edited by: Andres Gonzalez ]
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Floating point literals are defined as double This means that 1.0 will always be a double value and not a float value. In order to specify a float, you must append 'f' or 'F' after the value, as in 1.0f or 1.0F Actually the int value is widened to a float value as per JLS 5.1.2 Widening Primitive Conversion
"1" is a valid IntegerLiteral (see eventually JLS-2.0 chapters: 5.18.2 and 3.10.1 for more info). So the value corresponding top the "1" literal is an int type (having value), which is implicitly converted to a float. So: float f = 1; // valid, "1" is an IntegerLiteral float f = 1.0; // INVALID, "1.0" is a FloatingPointLiteral, that does not end with 'f' nor 'F'