Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Replace a char in a String

 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • 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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except for the useless first call to stringReplace that's just the way to go.
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic