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?
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