• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt with String method "replace"

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://coderanch.com/t/264749/java-programmer-SCJP/certification/Quote-your-sources
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A similar question has already discussed here.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
{
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
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here s1 is runtime object not compile time object.
so it is giving false.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
source:

Amit Poddar-Q11
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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');

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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic