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

[1024 byte] By [java4life87a] at [2007-10-2 1:48:41]
# 1
String.replaceAll
CeciNEstPasUnProgrammeura at 2007-7-15 19:29:52 > top of Java-index,Java Essentials,Java Programming...
# 2
How could that help. I want to get rid of the @, not replace any letters.
java4life87a at 2007-7-15 19:29:52 > top of Java-index,Java Essentials,Java Programming...
# 3
You could replace it with nothing.
CeciNEstPasUnProgrammeura at 2007-7-15 19:29:52 > top of Java-index,Java Essentials,Java Programming...
# 4
And if the @ is only at the end, you could as well use substring().
CeciNEstPasUnProgrammeura at 2007-7-15 19:29:52 > top of Java-index,Java Essentials,Java Programming...
# 5
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?
java4life87a at 2007-7-15 19:29:52 > top of Java-index,Java Essentials,Java Programming...
# 6
> 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?Huh?if(s.endsWith("@")) s = s.substring(0, s.length - 2);
CeciNEstPasUnProgrammeura at 2007-7-15 19:29:52 > top of Java-index,Java Essentials,Java Programming...
# 7

> 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]

jesperdja at 2007-7-15 19:29:52 > top of Java-index,Java Essentials,Java Programming...
# 8
> 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.
da.futta at 2007-7-15 19:29:52 > top of Java-index,Java Essentials,Java Programming...
# 9

> > 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 > top of Java-index,Java Essentials,Java Programming...