wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals 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 "equals" Watch "equals" New topic
Author

equals

Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
This question is from one of the mock exams..
Float F1 = new Float(0.9F);
Float F2 = new Float(0.9F);
Double D1 = new Double (0.9);
Which of the following will return true?
(1) (F1 == F2)
(2) (F1 == D1)
(3) F1.equals(F2)
(4) F1.equals(D1)
(5) F1.equals(new Float(0.9F))
I thought 3,4 & 5 are correct but the correct answer was 3 & 5.
My understanding is that the equals methods compares the actual value of the objects. In this case both F1 & D1 have 0.9 so why is 4 wrong?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
right but before comparing the values, the equals method checks that the argument is of the same type than the object upon which the method is invoked, i.e. Float. If not, false is returned. Check the source code !
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Roopa,
See if this link is of any use... http://www.javaranch.com/ubb/Forum24/HTML/012847.html
Shyam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals