• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String replace method

 
Naresh Saw
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nilesh Raje
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sameer inamdar
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Naresh Saw
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya Hope this solves your doubt
 
Naresh Saw
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think u r right. this might be happening behind the scene.

thanks a lot
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic