• Post Reply Bookmark Topic Watch Topic
  • New Topic

Number Casting - [Jeanne Boyarsky Page 357]  RSS feed

 
jnrohit Jain
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following code will not compile because of line 4 and 6. I was expecting line #7 will also give compile time error because it is having long (64 bit) value which is bigger than float integral value 32-bit. Why line #7 working correct?



 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jnrohit Jain wrote:I was expecting line #7 will also give compile time error because it is having long (64 bit) value which is bigger than float integral value 32-bit. Why line #7 working correct?


Java uses the actual value, and not the size (in memory) to determine which is larger... and it is also the range, so both the minimum and maximum value matters. Anyway...

The maximum value of a signed long is ... 9,223,372,036,854,775,807.

The maximum value of a single precision float (not counting infinity) is ... 3.402823 × 10^38 ... or extending it out is ... 340,282,300,000,000,000,000,000,000,000,000,000,000.

I would say that float is bigger than long...

Henry
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether we think a long is bigger than a float or vice versa makes little difference; the Java® Language Specification decrees it, so long→float is a permissible widening primitive conversion.
Somebody has to make decisions like that when writing a language; the more specific the decision, the more robust the language will be.
 
jnrohit Jain
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow.... Good to know that..
Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!