if string objects are interned then why change in one does not affect other
public class EqualExample {
public static void main(String[] args) {
String str = new String("Hello");
String str1 = new String("Hello");
System.out.println(str == str1);
System.out.println(str1.equals(str));
}
}
Output of above programe would be
false true
public class EqualExample {
public static void main(String[] args) {
String str = "Hello";
String str1 = "Hello";
System.out.println(str == str1);
System.out.println(str1.equals(str));
}
}
Output of above code is
true true
this is because in string pool Heloo alredy exists so it intern the string and refer to same object then why if i change str1 to "heloo java" then why str still have value "heloo". because they refer to same objects so the value of str must be change public class EqualExample { public static void main(String[] args) {
String str = "Hello";
String str1 = "Hello";
System.out.println(str == str1);
System.out.println(str1.equals(str));
str1="Heloo java";
System.out.println(str+str1);
System.out.println(str == str1);
System.out.println(str1.equals(str));
}
}
output true true HelooHeloo java false false