We should be clear that equals() method and == operator perform two different operations.The equals() method compares the character inside a string whereas == operator compares two object reference to see whether they refer to the same instances.Here is a small program to demonstrate the difference between equals()method vs == operator:
package stringdemo;
public class Equal {
public static void main(String[] args) {
String s1="john";
String s2=new String(s1);
System.out.println(s1.equals(s2));
System.out.println(s1==s2);
}
}
The above program gives the following output:
true
false
0 comments:
Post a Comment