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
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: -0.0 == 0.0 ? help
 
Similar Threads
double question in srinivas majji's mock exam
Majji #Q.28
A question from Mock
confusion on (==)
Math functions