aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why equals methods printing false ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why equals methods printing false ?" Watch "why equals methods printing false ?" New topic
Author

why equals methods printing false ?

Raju Sri
Ranch Hand

Joined: Mar 10, 2004
Posts: 108
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


SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3<br />SCDJWS 1.4
Thomas De Vos
stable boy
Ranch Hand

Joined: Apr 12, 2003
Posts: 425
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.


Try your free <a href="http://www.javacertificate.com" target="_blank" rel="nofollow">SCJP 1.4</a> certification centre.<br />Try your free <a href="http://www.j2eecertificate.com" target="_blank" rel="nofollow">SCWCD</a> certification centre.<br />Try your free <a href="http://www.ejbcertificate.com" target="_blank" rel="nofollow">SCBCD</a> certification centre.<br />Try your <a href="http://www.webspherecertificate.com" target="_blank" rel="nofollow">Websphere (Test 285) </a> certification centre.<br />Try your <a href="http://www.j2mecertificate.com" target="_blank" rel="nofollow">SCMAD</a> certification centre. (New)<br /> <br /><a href="http://blogs.javacertificate.com" target="_blank" rel="nofollow">Java/J2EE Certification Blogging</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why equals methods printing false ?