• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals

 
Roopa Bagur
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roopa,
See if this link is of any use... http://www.javaranch.com/ubb/Forum24/HTML/012847.html
Shyam
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic