This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Float literals. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Float literals." Watch "Float literals." New topic
Author

Float literals.

Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
Java 1.4//
Why am I getting error on line 3 and not one line 1 and 2?



Thanks,
--

Shivani,
[ September 25, 2005: Message edited by: Shivani Chandna ]

/** Code speaks louder than words */
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Please check your question.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
On the line 3 you have an invalid digit. You have the letter "l" (el) and not a "1" (one)
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
Sad - :-/ what an error ! :roll:
I looked up - on EditPlus both l and 1 were looking alike .
Jayant Raj
Ranch Hand

Joined: Mar 11, 2004
Posts: 42
Can you please explain the type conversions taking place here?
According to my understanding,



will throw a compile time error. If we dont append 45.64e with f, it will implicitly be a double.Thus the right hand expression will be a double which cannot be assigned to a float.

Please clarify.


regards,<br />Jayant Raj<br /> <br />SCJP 1.4 [98%]<br />SCWCD 1.4 [91%]<br />SCBCD [In Progress]
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,


There is no compilation error...As you said

45.64e is double and hence the whole expression would be implicitly a double expression...

After the evaluation it is converted back to float..

here 45.64e is within the range of float and hence there is no compilation error.

But if the evaluated value exceeds the range of float it needs to be

explicitly casted else a compilation error..



An interesting thing in your example...




If the literal is defined as 1.0f then error occurs ..Why???



Regards
[ September 26, 2005: Message edited by: A Kumar ]
Joji Doi
Greenhorn

Joined: Jun 23, 2005
Posts: 3
>>If the literal is defined as 1.0f then error occurs ..Why???
Look at the following as a float literal rather than literals.

float f1 =45.64e-1f;

The above statement is NOT 45.64e - 1f as minus arithmetic.
45.64e-1 is double literal. The exponent cannot be floating point.

Thus the following is equivalent:
float f1 =(float)45.64e-1;

Cheers!
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

That means my explanation above is not in tune with the above code...


I overlooked that


Regards
Jenny Thompson
Greenhorn

Joined: Sep 27, 2005
Posts: 1
Hi ,
The error is nothing bcoz of logical thing i belive..
when i compiled ur code i got the following error message..


Test.java:5: ';' expected
float f3 =45.l4e-1;
^
1 error

instead of numerical value one(1)in ur code lowercase of L is present..
it caused the problem..if u change it to one it s compiling nicely...
no error occurs.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1527
Guys, before you answer, don't just read the question alone, do also read the opinions and discussion that follows under it. I guess the author has already discovered the fault.

Originally posted by Shivani Chandna:
Sad - :-/ what an error ! :roll:
I looked up - on EditPlus both l and 1 were looking alike .

[ September 27, 2005: Message edited by: Akhil Trivedi ]

Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
 
jQuery in Action, 2nd edition
 
subject: Float literals.