File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question from a mock exam

 
Ratna Singh
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am puzzling over the following question
. The call to t.piggy(sName) does not modify the static String member. Compiling and running confirms it but I don't understand how come the println displays vandeleur or vandeleur 0 1 2 3 etc and not vandeleur wiggy or vandeleur wiggy 0 1 2 3.
Thanks

public class Tux extends Thread{
static String sName = "vandeleur";
public static void main(String argv[]){
Tux t = new Tux();
t.piggy(sName);
System.out.println(sName);

}
public void piggy(String sName){
sName = sName + " wiggy";
start();
}
public void run(){

for(int i=0;i < 4; i++){
sName = sName + " " + i;

}
}
}
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ratna,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you for your cooperation.
 
Ratna Singh
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry about it, I have corrected my profile.
Thanks,
 
Anup Engineer
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I think:
when you make a call to
public void piggy(String sName){
sName = sName + " wiggy";
start();
}
the string you pass is stored in the local variable sName, which is a completely different variable. Whatever changes that take place here wont affect the original string, which is used int he run() method. Hence the result.
I hope this answers the question, and its a correct explanation
 
Ratna Singh
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anup, thanks for the response. I am still not clear, because I thought when String objects are passed as parameters, a copy of their reference is passed, so any changes made to that reference will modify the original Object.
Ratna
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so any changes made to that reference will modify the original Object.
String objects are immutable. You can't change their content once initialized.
So
sName + "wiggy"
will yield a new String object which is assigned to a local variable.
Moreover, even if String objects were mutable, sName+"wiggy" would not have any effect on sName since you don't invoke any method on it and thus don't act on the reference.
 
Anil Rudraraju
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U r right ratna, thats exactly what happens ie. a copy of the sname String reference is passed to piggy() and incidently it is also called sname in the example, so here there r 2 "sname" reference variables.... both refering to "vandeleur". Now when u do concatenation, a new String object is created and the local sname variable will now refer to "vandeleur wiggy" while the original is still pointing to "vandeleur" hence the o/p.
HTH
 
Ratna Singh
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a ton for the responses. I have started taking these mock exams after reading from books and refreshing Java learnt from school and it is a real eye opener. Gosh, I KNEW Strings are immutable and that assigning to references in a method gets lost when the record pops but wasn't putting it together. Thanks again, Javaranch.
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ratna
Note that the run method modifies the value of the string field because it access the field. It doens't create a local variable. If the following is added enough time is given for the thread to finish:
try{
Thread.currentThread().sleep(5000);
}
catch (InterruptedException e) {}
System.out.println(sName);
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic