wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about assignments... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about assignments... " Watch "question about assignments... " New topic
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about assignments...
 
Similar Threads
JQ+
In case you missed it...
transient and static
Is this a bug?
error in Marcus Green 2.