What is difference between String equals() , == and intern() methods
Here we have some differences between equals() and ==
1. equals() is a method and == is operator for comparison
of two string objects.
2. == checks if both objects point to the same memory location whereas .equals() evaluates the comparison of values in the objects.
3. intern() method points the equal object to the same memory reference in the string pool.
public class StringInturn {
public static void inturnTest() {
String s1="Mukesh";
String s2=new String("Mukesh");
String s3="Vidhata";
String s7=s2.intern();
String s="Mukesh";
System.out.println( s1==s);
System.out.println("s1==S2" +s1==s2);
System.out.println("s1.equal(s2):" +s1.equals(s2));
System.out.println("s1.equals(s3):"+s1.equals(s3));
System.out.println("s1==s7:" +(s1==s7));
//Here s7of s2 points same object in the spring pool
as s1 so it s1==S7return true
}
public static void main(String [] args) {
inturnTest();
}
}
Programm output
false
false
s1.equal(s2):true
s1.equals(s3):false
s1==s7:true
No comments:
Add your comment