aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String replace method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String replace method" Watch "String replace method" New topic
Author

String replace method

Naresh Saw
Ranch Hand

Joined: Sep 03, 2005
Posts: 66
hi friends

i have code here. the line 6 should print true but its printing false.
why?

1. String s1 = "Naresh";
2. String s2 = "Naresh";

3. System.out.println(s1==s2); //printing true

4. String s3=s1.replace('N', 'G');
5. String s4=s2.replace('N', 'G');

6. System.out.println(s3 == s4); // printing false.


naresh<br />SCJP 1.4(86%), SCWCD 1.4(78%)
Nilesh Raje
Ranch Hand

Joined: Aug 02, 2005
Posts: 153
Hello Friend,


1. String s1 = "Naresh";
2. String s2 = "Naresh";

3. System.out.println(s1==s2); //printing true

4. String s3=s1.replace('N', 'G');
5. String s4=s2.replace('N', 'G');

6. System.out.println(s3 == s4); // printing false.

This is right..! cause String is Immutable..! Replace method returns a new string..!

s1 and s2 refer to "Naresh" which are in same pool so they are ==
s3 and s4 creates different string objects hence it returns false..!

if you do s3.equals(s4) u will get true cause the contents are been compared..!

Hope this helpss


Thanks and Regards,<br />Nilesh<br />SCJP 1.4, SCWCD 1.4
sameer inamdar
Ranch Hand

Joined: Jul 04, 2005
Posts: 40
As far as my explaination goes I think

when you create
S1 string is created and reference is stored in s1
when you create s2 with same string JVM doesn't create string at another location but stores the reference of earlier string in s2
Thus s1 == s2 returns true;

but when you manupulate
s1 and store in s3 string is created at new location and with the desire change. thus s3 has different reference

same with s2 s4 is created with new ref.
and thus s3 == s4 false as s3 and s4 are not same

Hope its correct please anyone can justify this


Dream!!! Set Vision !! Transform Vision to Mission by Actions on Deadlines!.
Naresh Saw
Ranch Hand

Joined: Sep 03, 2005
Posts: 66
Thanks Nilesh & Sameer for reply

but s3 and s4 have same value like :
s3="Garesh"
s4="Garesh"

so as per my knowledge both of these should refer to the same object and hence reference equality should return true.
i.e. (s3==s4) = true.
Mat Williams
Ranch Hand

Joined: Jul 20, 2005
Posts: 215
Hi,

I think the thing to remember is that a String object is just a pointer (in C speak). That is the JVM maintains a pool of Strings and you can have many many String Object just pointing at the same instance of a string in the JVM, but each of the String objects is a different object instance, therfore different memory location.

When you do a == you are comparing the memory location of the String Object not the underlying character representation of the String.

What is happening is that compiler and JVM are assigning s1 and s2 the same String object. You can see this by doing the following


the moment you do

you are creating new String objects (look at the API for the replace method). Therefore the == will compare the memory location of the different String objects and return false, whereas s3.equals(s4) should return true.

HTH

Mat
sameer inamdar
Ranch Hand

Joined: Jul 04, 2005
Posts: 40
Ya Hope this solves your doubt
Naresh Saw
Ranch Hand

Joined: Sep 03, 2005
Posts: 66
i think u r right. this might be happening behind the scene.

thanks a lot
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String replace method