• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about assignments...

 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Miki Muzsi
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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'
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic