• Post Reply Bookmark Topic Watch Topic
  • New Topic

Data Types  RSS feed

 
geeta rai
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Geeta,
Working with money written by Thomas Paul is a good article. This helped me a lot.
 
geeta rai
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!