File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why Integer is behaving differently ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why Integer is behaving differently ??" Watch "why Integer is behaving differently ??" New topic

why Integer is behaving differently ??

Raju Sri
Ranch Hand

Joined: Mar 10, 2004
Posts: 108
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 ?

(Added code tags - Barry)
[ April 18, 2004: Message edited by: Barry Gaunt ]

SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3<br />SCDJWS 1.4
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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.

[Jess in Action][AskingGoodQuestions]
Raju Sri
Ranch Hand

Joined: Mar 10, 2004
Posts: 108
Hi Ernest,
Thanks for your explanation.
I agree. Here's the link:
subject: why Integer is behaving differently ??
It's not a secret anymore!