File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# equals

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi!
2) Given the following code,
Float f = new Float(0.9F);
Float g = new Float(0.9F);
Double d = new Double(0.9);
Which of the following conditions are valid and will return true?
a. (f==g)
b. (g==d)
c. (f.equals(new Float(0.9F))
d. (f.equals(d))
e. (f.equals(g))
The correct answer is c and e, but why not d???
Greenhorn

Joined: Aug 07, 2001
Posts: 1
Hi,
This is beca's equals() method of Object class is overridden in Float class and it checks whether the augument passed is instanceof Float.
If not, equals() returns false without comparing values wrapped.
Try to look into source code of Float.equals().
I hope it will make it more clear.
Originally posted by Jordi Marqu�s:
Hi!
2) Given the following code,
Float f = new Float(0.9F);
Float g = new Float(0.9F);
Double d = new Double(0.9);
Which of the following conditions are valid and will return true?
a. (f==g)
b. (g==d)
c. (f.equals(new Float(0.9F))
d. (f.equals(d))
e. (f.equals(g))
The correct answer is c and e, but why not d???

kavita desai
Greenhorn

Joined: Jul 09, 2001
Posts: 16
d is false because u cannot use equals() to compare 2 non realted classes. but if u do, there is no error it simply returns false.
hope that helps
kavita desai
Siobhan Murphy
Ranch Hand

Joined: Oct 19, 2000
Posts: 72
I still can't understand why
f == g; is false, but
f.equals(g); is true.
Could someone please explain?
Is it that == tests if the references denote the same object, unless they are primitives, then it tests if they hold the same value? Whereas .equals does the same except for Wrappers and Strings where it tests if they hold the same value?

Eddy Pagotto
Greenhorn

Joined: Jul 04, 2001
Posts: 5
Siobhan,
this is possible because the equals() is overidden in all classes (so also the wrapper ones like Float, Double, Integer, and so on), but not in Object and StringBuffer, where equals() has the same effect as ==.
I hope this helps
Eddy
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
QUOTE
-------------------------------------
I still can't understand why
f == g; is false, but
f.equals(g); is true.
Could someone please explain?
Is it that == tests if the references denote the same object, unless they are primitives, then it tests if they hold the same value? Whereas .equals does the same except for Wrappers and Strings where it tests if they hold the same value?
------------------------------------
My observations..
1. == always checks for object reference equality.

2. that is it will check whether both objects refer to the same address.
3. Only in case of primitives it checks the values, but we will ignore that for our current discussion.
4. equals() also does the same thing; except for the classes that had overridden equals().
5. that is, equals also will check for object reference like == in all classes except STRING & WRAPPER classes.
6. In these two classes equals() can be used to check the contents. Such comparison is called as DEEP COMPARISON against the object reference comparison done by == which is known as SHALLOW COMPARISON.
7. In the case of wrapper class when equals() is applied,
first it will check whether the parameter passed is an object.
second it will check whether the objects being compared belong to the same class.
third it will look for the contents.
If all the three steps are satisfied then it will report true else false.
Now in this context u try to analyse the problem under reference.
when an object of double is compared with an object of float., the test fails in step 2. Hence the result.
HTH & u r clear.