| Author |
question about assignments...
|
Andres Gonzalez
Ranch Hand
Joined: Nov 27, 2001
Posts: 1561
|
|
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
|
 |
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
|
|
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
|
SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
|
 |
Miki Muzsi
Ranch Hand
Joined: Jun 23, 2003
Posts: 120
|
|
"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'
|
Miki<br /> <br />SCJP 1.4, SCBCD 1.3
|
 |
 |
|
|
subject: question about assignments...
|
|
|