Possible Duplicate:
what the difference between the float and integer data type when the size is same in java?
As you probably know, both of these types are 32-bits.int can hold only integer numbers, whereas float also supports floating point numbers (as the type names suggest).
How is it possible then that the max value of int is 231, and the max value of float is 3.4*1038, while both of them are 32 bits?
I think that int‘s max value capacity should be higher than the float because it doesn’t save memory for the floating number and accepts only integer numbers. I’ll be glad for an explanation in that case.
Your intuition quite rightly tells you that there can be no more information content in one than the other, because they both have 32 bits. But that doesn’t mean we can’t use those bits to represent different values.
Suppose I invent two new datatypes,
uint4andfoo4.uint4uses 4 bits to represent an integer, in the standard binary representation, so we haveBut
foo4uses 4 bits to represent these values:Now
foo4has a much wider range of values thanuint4, despite having the same number of bits! How? Because there are someuint4values that can’t be represented byfoo4, so those ‘slots’ in the bit mapping are available for other values.It is the same for
intandfloat– they can both store values from a set of 232 values, just different sets of 232 values.