File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about assignments... " Watch "question about assignments... " New topic

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

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

[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.
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:
subject: question about assignments...
It's not a secret anymore!