• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

wrapper class

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai,
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
but,
boolean b=(0.0==-0.0);//returns true
regards
udhaya
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
udhaya,
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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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.

Rgds,
Mikael.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic