It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wrapper class 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 "wrapper class" Watch "wrapper class" New topic

wrapper class

udhaya kanagaraj
Ranch Hand

Joined: Mar 31, 2005
Posts: 62
can any one explain me,why the below code returns false
Float f1 = new Float(+0.0f);

Float f2 = new Float(-0.0f);
System.out.print(f1.equals(f2));//returns false
boolean b=(0.0==-0.0);//returns true
M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
this is what i'm guessing. you are doing a == on literals, but invoking the overriden method equals on the other. so, its likely that Float's overriden equals method is implemented to be not equal for 0.0 and -0.0
Mikael Bellec

Joined: Nov 27, 2001
Posts: 6

the Float equals method use the native method floatToIntBits to do its comparison.
I take it the result of comparison between Float(-0.0) and Float(0.0) might be different according to the platform implementation of this method.

However this does not seem very logical to me.

Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
You've found a slight inconsistency in the language. According to JLS 15.21.1 Numerical Equality Operators == and !=:
Positive zero and negative zero are considered equal. Therefore, -0.0==0.0 is true, for example.

However, as Mikael Bellec cleverly noted, Float.equals() uses Float.floatToIntBits() to do its comparison. In the Java doc for Float, it mentions the inconsistency:

...two float values are considered to be the same if and only if the method floatToIntBits(float) returns the identical int value when applied to each.

Note that in most cases, for two instances of class Float, f1 and f2, the value of f1.equals(f2) is true if and only if

f1.floatValue() == f2.floatValue()

also has the value true. However, there are two exceptions:

If f1 and f2 both represent Float.NaN, then the equals method returns true, even though Float.NaN==Float.NaN has the value false.
If f1 represents +0.0f while f2 represents -0.0f, or vice versa, the equal test has the value false, even though 0.0f==-0.0f has the value true.
This definition allows hash tables to operate properly.
--Float.equals() Java doc

At the low level, floatToIntBits() returns a bit sequence starting with 0 for a positive zero value, and floatToIntBits() returns a bit sequence starting with 1 for a negative zero value. The two bit sequences are unequal so the equals() test returns false. The reason the authors chose to do it this way is to ensure that for any pair of Float objects where equals() returns true, the hashCode() return values will be equal. The hashCode() method returns
the integer bit representation, exactly as produced by the method floatToIntBits(float), of the primitive float value represented by this Float object.
--Float.hashCode() Java doc

SCJA 1.0 (98%), SCJP 1.4 (98%)
I agree. Here's the link:
subject: wrapper class
It's not a secret anymore!