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