Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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: 23877
142
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: 69400
276
  • 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!
 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic