Assuming written inside main
String s1 = new String("amit");
System.out.println(s1.replace('m','r'));
System.out.println(s1);
String s3="arit";
String s4="arit";
String s2 = s1.replace('m','r');
System.out.println(s2==s3);
System.out.println(s3==s4);
Above program segment produces following output:
arit
amit
false
true
i thought it will be:
arit
amit
true
true
I thought that Strings 's2' and 's3' will be pointing to the same String literal "arit" in String constant pool. But its giving the output as false when s2 and s3 are compared with == operator. can anybody explain why?