• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why Integer is behaving differently ??

 
Raju Sri
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
Can somebody pls explain me about the output of below code.

Output of above code is
equal unequal equal equal equal

why Integer.MAX_VALUE compare method is displaying unequal ?
Cheers
Raju

(Added code tags - Barry)
[ April 18, 2004: Message edited by: Barry Gaunt ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Floating-point values are not exact; they're represented in a fixed number of bits which, for float, is smaller than the number of bits needed to represent Integer.MAX_VALUE exactly. Therefore, the float value will be "rounded" to the nearest representable value. double, though, does have enough bits to represent this value, so it will contain the exact value. Thus, the two variables will contain different numbers.
For Long.MAX_VALUE, neither float nor double has enough bits to represent the value exactly; it's just luck, I think, that the rounded values are the same for both float and double.
 
Raju Sri
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest,
Thanks for your explanation.
Raju
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic