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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Data Types" Watch "Data Types" New topic
Author

Data Types

geeta rai
Ranch Hand

Joined: Sep 18, 2003
Posts: 48
float a = 1; // 1
float b = 1L; // 2
float c = 1F; // 3
float d = 1.0; // 4
Compile time error occurs at 4. Why is //2 valid and //4 is not?
Sagarika nair
Ranch Hand

Joined: Aug 13, 2003
Posts: 39
Hi Geeta!
float a = 1; // 1
float b = 1L; // 2
float c = 1F; // 3
float d = 1.0; // 4
All floating point values are double by default.So in line 4 ,1.0 is a double and you are assigning it to a float(32 bits) which is a smaller container compared to double(64 bits).So it gives a loss of precision error.It can be compiled by applying a cast as follows:
float d=(float)1.0
or by appending an f to the end of 1.0 i.e float d=1.0f
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
Hi Geeta,
Working with money written by Thomas Paul is a good article. This helped me a lot.
geeta rai
Ranch Hand

Joined: Sep 18, 2003
Posts: 48
Hi Cathy,
I tried to go through the article but it seems a little difficult. Seems i need to study more on data types.
Sagarika, thanks for ur reply but the explaination you gave didn't clear my doubt. If a float is double implicitly and because of loss of precision the compile time error is generated then why is it not the case with Long?
 
jQuery in Action, 2nd edition
 
subject: Data Types