==
(DOUBLE EQUAL OPERATOR)
|
.EQUALS()
METHOD
|
It is an operator applicable for both primitives and object
references.
|
It is a method applicable only for object references but not for
primitives.
|
In the case of primitives == (double equal operator) meant for
content comparison, but in the case of object references == operator meant
for reference comparison.
|
By default .equals() method present in object class is also meant for
reference comparison.
|
We can't override== operator for content comparison in object
references.
|
We can override .equals() method for content comparison.
|
If there is no relationship between argument types then we will get
compile time error saying incompatible types.(relation means child to parent
or parent to child or same type)
|
If there is no relationship between argument types then .equals()
method simply returns false and we won't get any compile time error and
runtime error.
|
For any object reference r, r==null is always false.
|
For any object reference r, r.equals(null) is also returns false.
|
String s1 =
new String("ashok"); String s2 = new String("ashok");
System.out.println(s1==s2); //false
System.out.println(s1.equals(s2) );
//true
|
StringBuffer
s1 = new StringBuffer("ashok"); StringBuffer s2 = new
StringBuffer("ashok"); System.out.println(s1==s2); //false System.out.println(s1.equals(s2)
); //false
|
4. If r1.equals(r2) is false then r1==r2 is always false.
nice
ReplyDeletevery usefull
ReplyDelete