aspose file tools*
The moose likes Beginning Java and the fly likes Replace a char in a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Replace a char in a String" Watch "Replace a char in a String" New topic
Author

Replace a char in a String

Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99
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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

http://faq.javaranch.com/java/CallByReferenceVsCallByValue


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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."


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

Except for the useless first call to stringReplace that's just the way to go.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
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=?


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99
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

Joined: Jan 03, 2006
Posts: 490


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 ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Replace a char in a String