aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes -0.0 == 0.0 ? help 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 "-0.0 == 0.0 ? help" Watch "-0.0 == 0.0 ? help" New topic
Author

-0.0 == 0.0 ? help

sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
What will happen if you invoke the following method? B is the answer but why?
-------------
1: public void check()
2: {
3: System.out.println(Math.min(-0.0,+0.0));
4: System.out.println(Math.max(-0.0,+0.0));
5: System.out.println(Math.min(-0.0,+0.0) == Math.max(0.0,+0.0));
6: }

A) prints -0.0, +0.0 and false.
B) prints -0.0, +0.0 and true.
C) prints 0.0, 0.0 and false.
D) prints 0.0, 0.0 and true.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Zero is signed and hence negative zero is less than positive zero. However, for equality purposes( ie == ) they are equal!!.
Strange as it may seem, this is one of my favourite gotcha! 's
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
shanks iyer
Ranch Hand

Joined: Oct 16, 2000
Posts: 47
Thanks Ajith I also always semed to have this doubt.
Regards,
Shankar.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: -0.0 == 0.0 ? help