Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Number Casting - [Jeanne Boyarsky Page 357]

 
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?



 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
Marshal
Posts: 65019
246
  • 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!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!