Vous êtes sur la page 1sur 4

7/15/13

Doubt with String method replace (SCJP forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author
Gitesh Ramchandani Ranch Hand Joined: Feb 28, 2007 Posts: 274

Doubt with String method "replace"


posted 8/28/2007 4:41 PM

What is the output (Assuming written inside main) 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'); //WHY false? System.out.println(s2==s3); System.out.println(s3==s4); Ans: arit amit false true My doubt: Why is this false String s2 = s1.replace('m','r'); s1.replace('m','r') will create a new string "arit" which already exists in the string pool (which is s2), so why are we getting a false. pls help, regards, gitesh

www.coderanch.com/t/264812/java-programmer-SCJP/certification/String-method-replace

1/4

7/15/13

Doubt with String method replace (SCJP forum at JavaRanch)

Akhilesh Trivedi Ranch Hand Joined: Jun 22, 2005 Posts: 1493

posted 8/28/2007 4:46 PM

http://www.coderanch.com/t/264749/java-programmer-SCJP/certification/Quoteyour-sources

Keep Smiling Always My life is smoother when running silent. -paul [FAQs] [C ertification Guides] [The Linux Documentation Project] Abdullah Mamun Ranch Hand Joined: Mar 19, 2007 Posts: 99
I like...

posted 8/28/2007 4:46 PM

A similar question has already discussed here.

MooN

praveen oruganti Ranch Hand Joined: Feb 05, 2007 Posts: 73

posted 8/28/2007 5:27 PM

Originally posted by Gitesh Ramchandani: What is the output (Assuming written inside main) 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'); //WHY false? System.out.println(s2==s3); System.out.println(s3==s4); Ans: arit amit false true My doubt: Why is this false String s2 = s1.replace('m','r'); s1.replace('m','r') will create a new string "arit" which already exists in the string pool (which is s2), so why are we getting a false. pls help, regards, gitesh

public class IO
www.coderanch.com/t/264812/java-programmer-SCJP/certification/String-method-replace 2/4

7/15/13

Doubt with String method replace (SCJP forum at JavaRanch)

{ public static void main(String args[]) { 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'); //WHY false? System.out.println(s2); System.out.println(s2.equals(s3)); System.out.println(s3==s4); } } output: arit amit arit true true Just analyze the above example then you can understand the concept

Regards,<br />Praveen Oruganti<br />Software Programmer raghu nagabandi Ranch Hand Joined: Aug 14, 2007 Posts: 35 Gitesh Ramchandani Ranch Hand Joined: Feb 28, 2007 Posts: 274 Gitesh Ramchandani Ranch Hand Joined: Feb 28, 2007 Posts: 274

posted 8/29/2007 2:45 AM

here s1 is runtime object not compile time object. so it is giving false.

posted 8/29/2007 2:36 PM

source: Amit Poddar-Q11

posted 8/29/2007 2:56 PM

i came to understand two things: 1. s1.replace() is a run-time object. 2. If i have String s="gitesh"; and String s1="gitess"; String s2=s.replace('h','s');

www.coderanch.com/t/264812/java-programmer-SCJP/certification/String-method-replace

3/4

7/15/13

Doubt with String method replace (SCJP forum at JavaRanch)

then s2 will not point to the s1 string in the string pool, instead will create a new string "gitess", because "gitess" is not present at compile-time. Is my understanding correct?

Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp

subject: Doubt with String method "replace"

Similar Threads string doubt second one about string one more on == Doubt in Strings doubt about Strings from Whizlabs
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 15, 2013 07:41:50 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/264812/java-programmer-SCJP/certification/String-method-replace

4/4

Vous aimerez peut-être aussi