five exclamation marks behind a string .. is it impossible ?
I've wrote a method which I want to add five exclamation marks behind a String s.
here is my code :
/**
* Haengt fuenf Ausrufezeichen an den String an
*/
publicvoid ausrufezeichenAnhaengen(String s)
{
s = s +"!!!!!";
}
But when I want to try it out nothing happens ;(
Message was edited by:
JustLill
[550 byte] By [
JustLilla] at [2007-11-26 14:53:24]

It's not quite true that nothing happens there. You have a local variable inside that method. You compute a new value and assign it to that variable. Then the method ends and the local variable is discarded. So it's as if nothing ever happened, and as far as the calling method is concerned, nothing changes.
> I've wrote a method which I want to add five
> exclamation marks behind a String s.
>
> here is my code :
>
> /**
> * Haengt fuenf Ausrufezeichen an den String an
> */
> public void ausrufezeichenAnhaengen(String s)
> {
> s = s + "!!!!!";
> }
>
> But when I want to try it out nothing happens ;(
>
> Message was edited by:
> JustLill
okay, this is a different beast altogether.
In this case, it IS working, however, "s" is a local variable. Hence, when you return from the method ausrufezeichenAnhaengen, the parameter that was passed is NOT changed.
what you really need to do is this:
public String ausrufezeichenAnhaengen(String s) {
return s + "!!!!!";
}
This will return a string that is the contents of s, followed by !!!!!.
The root of this problem has to do with how java passes objects to methods -> by reference value. Hence, any modifications you make to the variable s do not effect the variable that you used to pass the value to the method in the first place.
- Adam