Top 20 Java Interview Questions
Wednesday 6 July 2016
Difference between "== operator" and "equals() method"
"= = operator" is for reference comparison
while
".equals() method" is for content comparison
for example:-
String s1= new String("Happy");
String s2= new String("Happy");
S.O.P (s1==s2) /// returns false ,as reference of both these are different
while
S.O.P (s1.equals(s2)) /// returns true , as content wise both these are same.i.e. both has same content( "Happy")
But there is a Loophole
while
".equals() method" is for content comparison
for example:-
String s1= new String("Happy");
String s2= new String("Happy");
S.O.P (s1==s2) /// returns false ,as reference of both these are different
while
S.O.P (s1.equals(s2)) /// returns true , as content wise both these are same.i.e. both has same content( "Happy")
Subscribe to:
Posts (Atom)