• 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:

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic