aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes string doubt second one Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "string doubt second one" Watch "string doubt second one" New topic
Author

string doubt second one

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
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"
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hi,
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.

Sandy


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?
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
yes you are right

again i made silly mistake
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string doubt second one