Originally posted by Karthik Rajashekaran: 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); //XYZ System.out.println(s3==s4);
Why is the out put at XYZ is false ?though both reference to same "arit"
I think both s2 and s3 do not refer to same object. s2 refers to a String Object in pool, whereas s3 refers to a new object created by the statement earlier: String s2 = s1.replace('m','r'); replace method always returns a new String Object even if that string is already present in the pool.
Correct me if I am wrong.
Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?