public class StringTest {
public static void main(String[] args) {
String str1 = new String("Pragya");
String str2 = new String("Pragya");
String str3 = str1;
String str4 = "Sumit";
String str5 = "Sumit";
System.out.println(str1 == str2);
System.out.println(str1 == str3);
System.out.println(str1.equals(str2));
System.out.println(str4 == str5);
System.out.println(str4.equals(str5));
}
}
Output :
false
true
true
true
true
In String class, equals() method has been overridden to compare the value of String, so it compares the String data and not the memory location.
public static void main(String[] args) {
String str1 = new String("Pragya");
String str2 = new String("Pragya");
String str3 = str1;
String str4 = "Sumit";
String str5 = "Sumit";
System.out.println(str1 == str2);
System.out.println(str1 == str3);
System.out.println(str1.equals(str2));
System.out.println(str4 == str5);
System.out.println(str4.equals(str5));
}
}
Output :
false
true
true
true
true
In String class, equals() method has been overridden to compare the value of String, so it compares the String data and not the memory location.
No comments:
Post a Comment