File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why equals methods printing false ?

 
Raju Sri
Ranch Hand
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Thomas De Vos
stable boy
Ranch Hand
Posts: 425
  • 0
  • Mark post as helpful
  • send pies
  • 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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic