aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question from a mock exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question from a mock exam" Watch "question from a mock exam" New topic
Author

question from a mock exam

Ratna Singh
Greenhorn

Joined: Mar 17, 2002
Posts: 19
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

Joined: Aug 26, 2001
Posts: 7610
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.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Ratna Singh
Greenhorn

Joined: Mar 17, 2002
Posts: 19
I am sorry about it, I have corrected my profile.
Thanks,
Anup Engineer
Ranch Hand

Joined: Mar 04, 2002
Posts: 48
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


Anup Engineer
Ratna Singh
Greenhorn

Joined: Mar 17, 2002
Posts: 19
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

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Mar 02, 2002
Posts: 7
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

Joined: Mar 17, 2002
Posts: 19
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

Joined: Jul 03, 2001
Posts: 2120
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);


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question from a mock exam
 
Similar Threads
Thread question
output of the thread
about thread
Thread - Please explain!
Threads Q