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.
Any comments...Welcome.
tvs sundaram