Académique Documents
Professionnel Documents
Culture Documents
Java FAQ
Recent Topics
Register / Login
Author
Gitesh Ramchandani Ranch Hand Joined: Feb 28, 2007 Posts: 274
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
Akhilesh Trivedi Ranch Hand Joined: Jun 22, 2005 Posts: 1493
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...
MooN
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
{ 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
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
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
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 .
www.coderanch.com/t/264812/java-programmer-SCJP/certification/String-method-replace
4/4