Originally posted by Vineet Sharma:
Hi to all,
I am sorry I typed the wrong code !
The right one is:
public class Test{
public static void main(String args[])
{
String s1 = new String ("amit");
String s3 = "arit" ;
String s2 = s1.replace('m','r');
System.out.println(s2==s3);
}
}
The answer to this code is false. Can some body explain?
I think it should be true and my reasoning is that the line "s1.replace('m','r')" will return a new string containing "arit". Now since the string "arit" already exists in the string pool (as created by s3) s2 will also start pointing to the same memory location. Therefore the variables s2 and s3 are now seen pointing to the same memory location and hence s2==s3 should be true!
Sorry for the long explanation. Once again thanks to all for the help...
Regards
Vineet
k_arulvel
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|