• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why equals methods printing false ?

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,
------------------------------------------------
float f1= -0.0f;
float f2= +0.0f;
System.out.println(f1==f2);
Float obj1=new Float("-0.0");
Float obj2=new Float("+0.0");
System.out.println(obj1.equals(obj2));
------------------------------------------------
The above code prints "true false" as output.
Why obj1.equals(obj2) printing false eventhough -0.0 and +0.0 are equal ?
Raju
 
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Positive zero and negative zero compare equal; thus the result of the expression 0.0==-0.0 is true and the result of 0.0>-0.0 is false
In the first example a positive floating point zero and a negative
floating point zero compare equal according to the JLS. The result of
the expression 0.0 == -0.0 is true.
The equals method of the Float object compares the bit pattern. If both are identical the equals method returns true. The equals method uses the floatToIntBits method to determine the equality.
I have used the following code to test the above statement.
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic