473,394 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

String replace

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
4 3537
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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

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

Similar topics

4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
13
by: dimitris67 | last post by:
How can I replace an occurence of p(a string) in an other string(s) with np(new string).. char* replace _pattern(char *s,char *p,char *np) PLEASE HELP ME!!!!!
7
by: VMI | last post by:
If I have the string "Héllo", how can I replace char (é) with an 'e'? I cannot use the String.Replace() fuction. It has to be by replacing one char with another. Thanks.
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
12
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" &...
9
by: Crirus | last post by:
dim pp as string pp="{X=356, Y=256}{X=356, Y=311.2285}{X=311.2285, Y=356}{X=256, Y=356}{X=200.7715, Y=356}{X=156, Y=311.2285}{X=156, Y=256}{X=156, Y=200.7715}{X=200.7715, Y=156}{X=256,...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
3
by: kronus | last post by:
I'm receiving an xml file that has a child called modified and it represents a date value in the form of a string -- Nov 14, 2008 -- and in my app, I have items associated with each object and I'm...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.