Originally posted by Gitesh Ramchandani:
What is the output (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'); //WHY false?
System.out.println(s2==s3);
System.out.println(s3==s4);
Ans:
arit
amit
false
true
My doubt: Why is this false String s2 = s1.replace('m','r');
s1.replace('m','r') will create a new string "arit" which already exists in the string pool (which is s2), so why are we getting a false.
pls help,
regards,
gitesh
public class IO
{
public static void main(String args[])
{
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'); //WHY false?
System.out.println(s2);
System.out.println(s2.equals(s3));
System.out.println(s3==s4);
}
}
output:
arit
amit
arit
true
true
Just analyze the above example then you can understand the concept