In general, we use == operator to compare references or addresses and equals() method is used to compare contents.
example:
String s1 = new String("java");String s2 = new String("java");System.out.println(s1 == s2);
System.out.println(s1.equals(s2));O/P:falsetrue
Here, the == operator compares the addresses of s1 and s2 which are clearly pointing to different address locations and hence the result of this comparison is false. Whereas, the equals() method compares the contents of s1 and s2 and hence result is true.
No comments:
Post a Comment