By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,783 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,783 IT Pros & Developers. It's quick & easy.

String replace

P: 4
Hi , i want the reason for the following output of replace function in java
1> System.out.println("String".replace('g','g') == "String".replace('g','g'));

2> System.out.println("String".replace('g','G') == "String".replace('g','G'));

output:
1>true
2>false
what is the reason for the above output.?
Jan 5 '07 #1
Share this Question
Share on Google+
4 Replies


10K+
P: 13,264
Hi , i want the reason for the following output of replace function in java
1> System.out.println("String".replace('g','g') == "String".replace('g','g'));

2> System.out.println("String".replace('g','G') == "String".replace('g','G'));

output:
1>true
2>false
what is the reason for the above output.?
First you should start a new thread if you have a problem.
In your problem the issue is more to do with == versus .equals than with the replace method.

String.replace returns a new String only if there was a replacement. In
("String".replace('g','g'), there is no replacement so "String" is returned with no new String being constructed. The right hand side of == is identical to this so again the literal "String" is returned. Now since both are literals and no new String has been created, the == comparison returns true.

In the second one "String".replace('g','G', a replacement is done and so a new String is created. The new String with literal value is a new object that is created and stored in memory. When the replace is called again at the RHS of == the same also happens. A new String is created. The new object created is also stored in memory at a different position than where the first one was stored. This means you now have two strings with value "StrinG" but stored at different memory locations. Now the comparison == compares references not values and since the references are different you get false. If however, you use the .equals method of String which compares values you will get true because this actually compares values.
Expand|Select|Wrap|Line Numbers
  1.  System.out.println("String".replace('g','g') == "String".replace('g','g'));
  2.   System.out.println("String".replace('g','G') == "String".replace('g','G'));
  3.   System.out.println(("String".replace('g','G')).equals(("String".replace('g','G'))));
This will be covered in more detail in the next Java class of http://www.thescripts.com/forum/thread581237.html
Jan 5 '07 #2

P: 4
First you should start a new thread if you have a problem.
In your problem the issue is more to do with == versus .equals than with the replace method.

String.replace returns a new String only if there was a replacement. In
("String".replace('g','g'), there is no replacement so "String" is returned with no new String being constructed. The right hand side of == is identical to this so again the literal "String" is returned. Now since both are literals and no new String has been created, the == comparison returns true.

In the second one "String".replace('g','G', a replacement is done and so a new String is created. The new String with literal value is a new object that is created and stored in memory. When the replace is called again at the RHS of == the same also happens. A new String is created. The new object created is also stored in memory at a different position than where the first one was stored. This means you now have two strings with value "StrinG" but stored at different memory locations. Now the comparison == compares references not values and since the references are different you get false. If however, you use the .equals method of String which compares values you will get true because this actually compares values.
Expand|Select|Wrap|Line Numbers
  1.  System.out.println("String".replace('g','g') == "String".replace('g','g'));
  2.   System.out.println("String".replace('g','G') == "String".replace('g','G'));
  3.   System.out.println(("String".replace('g','G')).equals(("String".replace('g','G'))));
This will be covered in more detail in the next Java class of http://www.thescripts.com/forum/thread581237.html

Thanks for the solution , but when the replace function encounters the old character will it check with the replacing character before doing the actual replacement ?
Jan 5 '07 #3

10K+
P: 13,264
Thanks for the solution , but when the replace function encounters the old character will it check with the replacing character before doing the actual replacement ?
Yes it compares with the new character using == because char is a primitive type there are no problems with the == comparison and no object is created there.
Jan 5 '07 #4

P: 1
Yes it compares with the new character using == because char is a primitive type there are no problems with the == comparison and no object is created there.

Just to add more information I am including one more case-

Intern Case :

System.out.println("String".replace('g','G').inter n() == "String".replace('g','G').intern());

will print "true".
Jan 10 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.