Help coderanch get a
new server
by contributing to the fundraiser
  • 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

How can used replace()?

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Happy3 {
static String s="Hello";
public static void main(String args[]) {
Happy3 h=new Happy3();
System.out.println(methodA(s));
}
public static String methodA(String s) {
s.replace('i','e');
s+=" World !!!" ;
return s;
}
}
It will output "Hello World!!!"
It was no used in above cord on replace.
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hades,
String object is immutable, i.e. not changable.
Any String method that needs to change the contents
will create a new String and return a reference to the
new String. If there is no change, a reference to the original
String is returned.
Whatever the case is, your code
simply has no effect on 's'.
[This message has been edited by Nain Hwu (edited December 04, 2001).]
 
Hades Pan
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it thank you !
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hades,
Nain is right in saying that s.replace('i','e') has no effect on 's' and is returning a reference to the original string. This is happening because
(1) there is no 'i' in 'Hello'. The definition for String replace is <code>String replace( oldChar, newChar )</code>.
(2) you are not storing the new string reference anywhere
Try the following
<code>
class Happy3 {
static String s ="Hello";

public static void main(String args[]) {
Happy3 h = new Happy3();
System.out.println(methodA(s));
System.out.println( s );
}
public static String methodA(String s) {
String str = s.replace('e','i');
str += " World !!!" ;
return str;
}
}

Output:
Hillo World !!!
Hello

</code>
A new String is created in methodA(), the original String 's' is not changed.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
Co-author Mike Meyers' Java 2 Certification Passport
 
Hades Pan
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK I see it Thank Jane Griscti very much~~!
 
My pie came with a little toothpic holding up this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic