This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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
 
Similar Threads
question about testing equal
Dan's question
about Float and Double
About Wrapper classes.
equals()