• 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

Replace a char in a String

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, this is my first post here. I'm having problem understanding 'replace'. Why doesn't this code work? Is there a syntax error in it?



The output is still: java. How do I make the code work?

Thank you.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://faq.javaranch.com/java/CallByReferenceVsCallByValue
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

The thing to consider here is that variables representing method parameters are local to the method. So "text" is local to the method stringReplace. At first, it references the same String object as "textString." But inside the method, "text" is reassigned to reference a new String produced by text.replace('j', 'c'). This assignment has no effect on the original String that's still referenced by "textString."
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Henry & marc,

Java object references are passed by value, i.e. it pass a 'copy' of the value to the method, therefore the original value is not changed. Ahhh, I see. No wonder it still prints "java".

I refactored the code to make it print "cava" by changing the void method to return a String value and then print that return value. Is that the correct way to replace a char in a String?



Thank you.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Except for the useless first call to stringReplace that's just the way to go.
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by LS chin:
Hi, this is my first post here. I'm having problem understanding 'replace'. Why doesn't this code work? Is there a syntax error in it?



The output is still: java. How do I make the code work?

Thank you.



Am I missing something here, doesn the String class have a replace method?



..or am I way off, and this is not what your trying (are you trying to override the replace Method=?
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stephen Davies:
Am I missing something here, doesn the String class have a replace method?

..or am I way off, and this is not what your trying (are you trying to override the replace Method=?


You're right. String class does have a replace method. Your code looks very neat.

The code that I gave in the OP came from an SCJP mock exam. I think that question was designed to "purposely" confuse us - making us think that the output would be "cava" but in fact, it is still "java".

The misleading part is this line:
stringReplace(textString);

- which serves no purpose, I would think.
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:
http://faq.javaranch.com/java/CallByReferenceVsCallByValue



That really isn't the reason why it didn't work. The reason is that String is immutable and a call to replace returns a new String.

To give an example of the same scenario where it would work:





This returns:

1
2

If String were mutable it would have worked, despite being passed by value.

The same code using StringBuffer:



This prints
Java
Cava

It is true that Java is always pass by value but that is not the solution to the question.
[ August 08, 2008: Message edited by: Rusty Shackleford ]
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rusty Shackleford:
If String were mutable it would have worked, despite being passed by value.


Thanks for the examples. I learnt something new again today. So, String objects are immutable. Ahh, I see.

Thanks.

It's interesting that there are so many ways to do the same thing in Java.

 
reply
    Bookmark Topic Watch Topic
  • New Topic