String passed into method

S

soni29

hi,
i had a question about the following program:

class StringTest {
String strTest = new String("Hello!");
public static void main(String args[]) {
StringTest obj = new StringTest();
obj.Display();
}

void Display() {
System.out.println("Before call " + strTest);
Update(strTest);
System.out.println("After call " + strTest);
}

void Update(String strString) {
System.out.println("In update method before " + strString);
strString = new String("Bye!");
System.out.println("In update method after " + strString);
}
}

why is it that at the end it prints: After call Hello! i changed the
reference in the method correct? i know String's are immutable but
doesn't the reference change in the method Update? as if i wrote
something like:

class Temp {
public static void main(String args[]) {
Temp obj = new Temp();
obj.CallMe();
}
void CallMe() {
String strName = new String("Name");
System.out.println(strName);
strName = new String("No Name");
System.out.println(strName);
}
}

in which case at the end it will print out No Name, since the
reference of the variable strName has now changed?

Thank you.
 
D

Darryl L. Pierce

soni29 wrote:

<snip>

First off, String is an immutable class. You cannot change the value of a
String object.

Secondly, Java does not pass-by-reference. It, instead, does a
pass-reference-by-value. IOW, the value of the reference that you're
passing to the method as an argument is itself *copied* and the method is
using that copy to reference you object.

As such, you can pass a reference to a String object in a method call, but
the invoked method, if it attempts to change the value, will instead be
working on a whole new String object. When the method exits, the original
reference value is unchanged. And, since it's unchanged, it's pointing to
the same String object as before the call.
 
E

Esoteric

what he says is true, but you could fix it by doing it like this:

class StringTest {
String strTest = new String("Hello!");
public static void main(String args[]) {
StringTest obj = new StringTest();
obj.Display();
}

void Display() {
System.out.println("Before call " + strTest);
strTest = Update(strTest); // <== changed
System.out.println("After call " + strTest);
}

String Update(String strString) { // <== changed return type from void to
String
System.out.println("In update method before " + strString);
strString = new String("Bye!");
System.out.println("In update method after " + strString);
return strString; // <== added return
}
}

hope this helps
owen.
 
D

Darryl L. Pierce

Esoteric said:
what he says is true, but you could fix it by doing it like this:

class StringTest {
String strTest = new String("Hello!");
public static void main(String args[]) {
StringTest obj = new StringTest();
obj.Display();
}

void Display() {
System.out.println("Before call " + strTest);
strTest = Update(strTest); // <== changed
System.out.println("After call " + strTest);
}

String Update(String strString) { // <== changed return type from void
to String
System.out.println("In update method before " + strString);
strString = new String("Bye!");
System.out.println("In update method after " + strString);
return strString; // <== added return
}
}

hope this helps

This assumes that the argument passed by Display() to Update() is a property
of the enclosing class. If, instead, it's just an argument and not a class
property, then making it a field is a bad design choice.

Also, my example highlights why he won't be able to do the same *between
classes* as well. Giving him a work-around in one class won't highlight the
underlying reason.
 
T

Thomas Schodt

This might not be the best way of doing it
but it demonstrates that an array of one element
allows you to pass back a new Object.


class StringTest {
String strTest = new String("Hello!");
public static void main(String args[]) {
StringTest obj = new StringTest();
obj.Display();
}

void Display() {
System.out.println("Before call " + strTest);
Update(strTest);
System.out.println("After call " + strTest);
}

void Update(String strString) {
System.out.println("In update method before " + strString);
strString = new String("Bye!");
System.out.println("In update method after " + strString);
}
}
 
T

Thomas Schodt

This might not be the best way of doing it
but it demonstrates that an array of one element
allows you to pass back a new Object.

class StringTest {
String[] strTest = { "Hello!" };
public static void main(String args[]) {
StringTest obj = new StringTest();
obj.Display();
}

void Display() {
System.out.println("Before call " + strTest[0]);
Update(strTest);
System.out.println("After call " + strTest[0]);
}

void Update(String[] strString) {
System.out.println("In update method before " + strString[0]);
strString[0] = new String("Bye!");
System.out.println("In update method after " + strString[0]);
}
}
 
D

Darryl L. Pierce

Thomas said:
This might not be the best way of doing it
but it demonstrates that an array of one element
allows you to pass back a new Object.

Ack! Unless the method receiving the argument can and will use an array,
it's poor design to pass an array just to get around the reference-by-value
constraint. If you're seriously in need of passing a mutable object, pass
StringBuffer, not an array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top