Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String woes

Status
Not open for further replies.

DoraC

Programmer
May 7, 2002
98
US
Hi,

I need to pass a String object to a class method, and have the String set in that method. However, while the string "appears" to be properly set in the method, upon return to the calling code it assumes its old value. For example:

Code:
public class A
{
...
    public void foo ()
    {
        String s = "first value";

        System.out.println("value is: " + s);

        B b = new B();
        b.Set(s);

        System.out.println("last is: " + s);
    };
....
}

public class B
{
....
    public boolean Set ( String s )
    {
        System.out.println("1 value is: " + s);
        s = "bar";
        System.out.println("2 value is: " + s);
    };
....
};

The output is then:
value is first value
1 value is first value
2 value is bar
last is first value

How can I have the class B method assign the new string "permanently" to s? I know strings are immutable, but it was (i thought) the case that they are referene objects - and hence can be assigned new references (even though what they point to can't be changed). Hence, the method call should create a new string object, and point the parameter-string to it...

I'm a relative newby to java, any/all help appreciated!
Thanks,
dora
 
Java Facts:
- objects are passed by reference, the reference itself is passed by value.
- primatives are passed by value.
- Strings are immutable (contents cannot be changed).

All this makes it impossible to do what you are trying. Let's look at a small example:
Say you create a String reference called s. This reference has a value, in other words a memory address. If we passed s to a method then the reference is passed by value, effectively making the object itself passed by reference. We can't change the actual content of the String (since it is immutable) so we need to change the value of the reference. However, when we pass by value, a copy of the value is made before the method call and regardless of what the method does that value remains UNCHANGED after the method exits. So even though your method changes the value of s, it reverts back to the "copied" value of s after returning.
Code:
String s = "Test"; // create a reference to memory location A
s = "New String"; // change reference to memory location B
set(s); // changes memory location to C
System.out.println(s);  // Still refers to memory location B

The thing to remember is when passing by value you can't directly affect the client variables. This design is for the safety of programmers so that they can be sure of the exact state of their code. Introducing pass by reference requires safety checks on the part of the developer. Check out Joshua Bloch's "Effective Java" for more discussion on this.

Back to the question at hand, to accomplish what you want you will need to use a StringBuffer instead of a String.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top