Changing Parameter contents
I know that you cannot change the value of a parameter. But I want to know of a way that I can change the actual variable. Here is the scenario:
// Enter a word
// Enter another word
// Enter a third word
// Check to see if the parameter contains an '@' at the end.
publicboolean correct(String theWord)
{
// Code not show
if (ThereIsAnAsterisks)
// remove it from the word
return true/false;
}
So I search to see String theWord has an '@', if it does I want to remove it from the instance variable. How can I do so?
Thank You
> I'd like to use substring but the problem is changing
> the value of the parameter, won't change the value of
> the actual variable. Am I mistaking?
No, you're not mistaken. In Java, parameters are passed by value.
[url=http://javadude.com/articles/passbyvalue.htm]Java is Pass-By-Value, Dammit![/url]
[url=http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html] Does Java pass by reference or pass by value?[/url]
[url=http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html]Passing Information into a Method or a Constructor[/url]
[url=http://www.bearcave.com/software/java/misl/no_references.html]Java: life in a pass-by-value world[/url]
> > So I search to see String theWord has an '@', if
> it
> > does I want to remove it from the instance
> variable.
> > How can I do so?
>
> Not at all. Strings are immutable.
So you'd have to pass a StringBuffer or StringBuilder, or else return a String.
In general, your original approach would work. If you'd used a mutable class, you could have modifie the contents of the object pointed to by the parameter, and the caller would see the changes.
jverda at 2007-7-15 19:29:52 >
