This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes assiging int to float Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "assiging int to float" Watch "assiging int to float" New topic
Author

assiging int to float

Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100


The ode compiles fine. That means int is imlpicitly castedto float.
I what to know why this happens?
Although both are 32 bits but then in float some bits may be reserved for the decimal part. Doesn't that leave a float with less number of bits for the integral part?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9946
    
    6

i'm not an expert, but i'll try to help.

as i understand it, the specification for floats basically reserves part of the 32 bits for the mantissa (all the numbers regardless of where the decimal point is), and part for the exponent. it's sort of like scientific notation. in other words, the values are stored something like

123456 04

would be 1234.56

123456 06

would be 123456.0

123456 18

would be 123456000000000000.0

as the number get very large, like my last example, there are actually larger and larger 'gaps' between values that can be stored. for all practiacl purposes, there is no difference between 123456000000000000.0 and 123456000000000001.0.

so, yes, there are fewer bits for the mantissa, but that's a trade off we make for being able to store floats.

does that help?


Never ascribe to malice that which can be adequately explained by stupidity.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Check this out.

Just keep in mind that you need not know all of these details for the SCJP exam. Knowing that casting from an int to a float is a widening conversion is enough for the exam.


SCJP Tipline, etc.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: assiging int to float
 
Similar Threads
Integer.MAX_VALUE
Loss of precision in primitives
int x = (int)floatNumber
Conversions
How the casting works for classes.