Вы находитесь на странице: 1из 4

7/15/13

Doubt with String method replace (SCJP forum at JavaRanch)

Doubt with String method replace (SCJP forum at JavaRanch) A friendly place for programming greenhorns! Big
Doubt with String method replace (SCJP forum at JavaRanch) A friendly place for programming greenhorns! Big

A friendly place for programming greenhorns!

Big Moose Saloon

Author Doubt with String method "replace" Gitesh Ramchandani posted 8/28/2007 4:41 PM Ranch Hand Joined:
Author Doubt with String method "replace" Gitesh Ramchandani posted 8/28/2007 4:41 PM Ranch Hand Joined:
Author Doubt with String method "replace" Gitesh Ramchandani posted 8/28/2007 4:41 PM Ranch Hand Joined:
Author Doubt with String method "replace" Gitesh Ramchandani posted 8/28/2007 4:41 PM Ranch Hand Joined:

Author

Doubt with String method "replace"

Gitesh
Gitesh

posted 8/28/2007 4:41 PM

Ranch Hand

Joined: Feb 28,

What is the output (Assuming written inside main) String s1 = new String("amit");

 

2007

System.out.println(s1.replace('m','r'));

System.out.println(s1);

Posts: 274

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

7/15/13

Doubt with String method replace (SCJP forum at JavaRanch)

 
Akhilesh    
Akhilesh    
 
 

posted 8/28/2007 4:46 PM

Ranch Hand

Joined: Jun 22,

 

2005

Posts: 1493

 

Keep Smiling Always — My life is smoother when running silent. -paul [FAQs] [Certification Guides] [The Linux Documentation Project]

 
 
Abdullah    
Abdullah    
 
 

posted 8/28/2007 4:46 PM

Ranch Hand

A similar question has already discussed here.

 

Joined: Mar 19,

 

2007

Posts: 99

 

I

MooN

 
  2007 Posts: 99   I like MooN   praveen     oruganti   posted
  2007 Posts: 99   I like MooN   praveen     oruganti   posted
 
praveen    
praveen    
 
 

posted 8/28/2007 5:27 PM

Ranch Hand

 

Joined: Feb 05,

     

2007

Originally posted by Gitesh Ramchandani:

 

Posts: 73

 

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

 

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
raghu

posted 8/29/2007 2:45 AM

Ranch Hand

Joined: Aug 14,

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

 

2007

Posts: 35

Gitesh

Gitesh
Gitesh

Ramchandani

posted 8/29/2007 2:36 PM

Ranch Hand

source:

Joined: Feb 28,

2007

Amit Poddar-Q11

 

Posts: 274

Gitesh

Gitesh
Gitesh

Ramchandani

posted 8/29/2007 2:56 PM

Ranch Hand

i came to understand two things:

 

Joined: Feb 28,

2007

1. s1.replace() is a run-time object.

2. If i have String s="gitesh";

 

Posts: 274

and String s1="gitess"; String s2=s.replace('h','s');

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

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

All times above are in your local time zone & format.The current ranch time (not your local time) is Jul 15, 2013 07:41:50.

Contact Us | Powered by JForum | Copyright © 1998-2013 Paul Wheaton