Win a copy of Head First Android this week in the Android 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Number Casting - [Jeanne Boyarsky Page 357]

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74371
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow.... Good to know that..
Thanks!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic