S.No
|
= = Operator
|
equals() method
|
1
|
It is used for reference comparison.
|
It is also used for reference comparison but if we want we can override for content comparison
|
2
|
It can be apply for both primitives and object references.
|
It can be apply for only object references.
|
3
|
s1==s2 is true, iff both s1 & s2 are pointing to the same object on the heap.
|
In the object class, .equals() returns true, iff both s1& s2 are pointing to the same object.
|
4
|
We can't apply == operator for incompatible types of objects violation leads to compile time exception saying incompatible types.
Ex: String s1="getsure"; Student s2 = new Student("suresh"); SOP(s1==s2) --> C.E |
It never rises any compile time / run time exception just it will simply returns false in the case of incompatible types.
|
5
|
In Identity Hashmap, JVM uses == operator to identify duplicate keys.
Ex: IdentityHashMap m=new IdentityHashMap(); Integer i1=new Integer(10); Integer i2=new Integer(10); m.put(i1, "GetSure");m.put(i2, "Infotech"); SOP(m) --> O/P: 10 - GetSure, 10 - Infotech |
In Hashmap, the JVM uses .equals() method to identify duplicate keys.
Ex: HashMap m=new HashMap(); Integer i1=new Integer(10); Integer i2=new Integer(10); m.put(i1, "GetSure");m.put(i2, "Infotech"); SOP(m) --> O/P: Only 10 - Infotech |
Wednesday, 18 November 2015
Difference between "== operator" and "equals() method" in java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment