wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Quest from Marcus Green Exam1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Quest from Marcus Green Exam1" Watch "Thread Quest from Marcus Green Exam1" New topic
Author

Thread Quest from Marcus Green Exam1

M Sharma
Ranch Hand

Joined: Dec 13, 2001
Posts: 106

The above code snippet refers to Question no. 19 of Marcus Green Exam1.
The Answer and explaination given to this question is :
4) Compilation and output of either "vandaleur", "vandaleur 0", "vandaleur 0 1" "vandaleur 0 1 2" or "vandaleur 0 1 2 3"
If that seems a vauge answer it is because you cannot be certain of the system that the underlying OS uses for allocating cycles for a Thread. The chances are that once the thread has been spun off in the call to start in the method piggy the main method will run to completion and the value of sName will still be vandeluer before the Thread modifies it. You cannot be certain of this though.
I ran this program on Windows NT and Solaris platform, i got two output's from the above given outputs... i.e. vandaleur and vandaleur 0 1 2 3.
My doubt is when the method
t.piggy(sName);
is called from the main method, it will return to main only after appending string "wiggy" to "vandaleur" and calling start. So when this method returns to main method it should append string "wiggy" to string "vandaluer".
So how does the output only shows vandaleur, as text appending is done inside piggy method and not inside a thread.
TIA
Manish


Regards, Manish
SCJP 1.4
Dhanashree Mankar
Ranch Hand

Joined: Aug 25, 2003
Posts: 123
In method piggy variable sName is local since it is redefined there as String.So when main method calls piggy ,a new copy of sName is created there so in main sName still contains "vandeleur" and not
"vandeleur wiggy"
When ur calling start method ur not sending a local copy of sName i.e. "vandeleur wiggy" . So in run method sName will take "vandeleur" only and proceeds further
M Sharma
Ranch Hand

Joined: Dec 13, 2001
Posts: 106
I had a second look at the code.
I feel that variable sName is being shadowed in method piggy. So due to variable shadowing the orignal sName variable which belongs to the class does not change. I think in this direction because if i change the variable name in the method signature as :
public void piggy(String sName1)
{
sName = sName + " wiggy";
start();
}
then it refers to the class level sName variable.
But when objects are passed to the method, a copy of its reference value is passed to the method. So will piggy method change the class level sName variable ??? as we passes the reference value of variable sName to the method piggy.
Pls Help !!!
[ September 12, 2003: Message edited by: Manish Sachdev ]
M Sharma
Ranch Hand

Joined: Dec 13, 2001
Posts: 106
Pls help ...
Uma Balu
Ranch Hand

Joined: Aug 22, 2003
Posts: 61
Original Code:


Modify the method parameter name


Hope this helps u understand. If the local variable name is the same as that of any member,the local variable takes the preference. If the name of the method parameter is different, the static member gets modified.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Manish Sachdev:
I feel that variable sName is being shadowed in method piggy. So due to variable shadowing the orignal sName variable which belongs to the class does not change.

Your assumption here is right. The class variable sName is being shadowed by the local variable sName defined in the method piggy. So what you are manipulating in that method is the local variable, and not the class variable.
You can actually test this by simply changing the signature of piggy to
public void piggy()
and modifying the call to to simply
t.piggy()
Originally posted by Manish Sachdev:
But when objects are passed to the method, a copy of its reference value is passed to the method. So will piggy method change the class level sName variable ??? as we passes the reference value of variable sName to the method piggy.

I need to be clear what you are really asking. If you are asking if a method(like piggy()) can change the value of an object, then the answer is yes if the object in question is mutable. The called method has a copy of the address of that object so it can do whatever it likes with it.
But if you are asking about the parameter itself, then the answer is it does not affect the caller. Parameters are passed-by-value, meaning, a copy of the value is made so the original will not be affected.
But when it comes to passing object reference, this may looks like passing by reference when in fact it is still pass-by-value. What is happening is that a copy of the address of the object is being made so the caller and callee will both have a copy of the address of the object.
Hope this helps.
[ September 15, 2003: Message edited by: Alton Hernandez ]
M Sharma
Ranch Hand

Joined: Dec 13, 2001
Posts: 106
Hi Alton,
Originally posted by Alton Hernandez:

But if you are asking about the parameter itself, then the answer is it does not affect the caller. Parameters are passed-by-value, meaning, a copy of the value is made so the original will not be affected.
But when it comes to passing object reference, this may looks like passing by reference when in fact it is still pass-by-value. What is happening is that a copy of the address of the object is being made so the caller and callee will both have a copy of the address of the object.

As far as my knowledge, i know that when primitives are passed to the method, a copy of the value of that primitive is passed to the Formal parameter of the method. But when objects are passed, as u said that the reference value or the address of that object is passed to that method.
I thought that the sName (class level variable) is not getting changed due to variable shadowing, and it seems to be true.
BUT, my knowledge also tells me that in case of object, a copy of the reference value is passed, so how can the sName variable be shadowed, as we are passing the reference value, the reference value will remain the same as of class level and it should refer the same object to what class level variable refers and hence should not be shadowed.
Please guide me.
[ September 18, 2003: Message edited by: Manish Sachdev ]
Mika Leino
Ranch Hand

Joined: Jan 07, 2002
Posts: 56
Manish, I also went wrong forst on that question. What I didn't at first remember is that Strings are immutable.
When the copy of the reference is sent to method piggy() it is referring to the same String object as the static class level variable. But when the String is altered inside the method the String itself is not changed but a second String will be created. And the reference inside the method is now referring to that new String.
Hope this helps.


Mika Leino<br />SCJP, SCWCD
M Sharma
Ranch Hand

Joined: Dec 13, 2001
Posts: 106
Hi Mika,
Yes, you are correct. Since Strings are immutable, it is creating a new object and class level variable does't change.
I tested the same program using StringBuffer and used append method for concatenating the string " wiggy", the program shows "vandeleur wiggy" as one of the possible outputs.
So the conclusion is :
  • If primitives are passed to the method, the copy of the value of that primitive is passed to the method.
  • If the object reference is passed to the method, always the copy of the reference value of that object is passed and with the help of that formal parameter of the method (to whom the ref. is assigned), one can acutally change the orignal object which is being passed to the method.

  • Please correct me if i am wrong anywhere.
    Thank you Alton, Dhanashree, Uma Balu & Mika for guiding me.
    Alton Hernandez
    Ranch Hand

    Joined: May 30, 2003
    Posts: 443
    Originally posted by Manish Sachdev:

    As far as my knowledge, i know that when primitives are passed to the method, a copy of the value of that primitive is passed to the Formal parameter of the method. But when objects are passed, as u said that the reference value or the address of that object is passed to that method.

    Actually, there is nothing different here regardless whether you are passing a primitive or an object reference. The parameters are still passed-by-value i.e. a copy of the parameter is made to the callee.
    Originally posted by Manish Sachdev:
    I thought that the sName (class level variable) is not getting changed due to variable shadowing, and it seems to be true.

    When you talk about shadowing, you are actually talking about scope or namespace. When you are inside the method, the sName variable that is in-scope is the parameter declared in that method. So the class variable sName is hidden at this method.
    Originally posted by Manish Sachdev:
    BUT, my knowledge also tells me that in case of object, a copy of the reference value is passed, so how can the sName variable be shadowed, as we are passing the reference value, the reference value will remain the same as of class level and it should refer the same object to what class level variable refers and hence should not be shadowed.

    You have to distinguish between modifying where a reference is pointing, and modifying the object itself.
    In the sample code below, the variable sb[/sb] and [B]sb1 both refer to the same object at line 1. And at line 2, method m2() is modifying that same object. However, at line 3, the sb1 now refers to a different object, but sb will not be affected by this.
    M Sharma
    Ranch Hand

    Joined: Dec 13, 2001
    Posts: 106
    Hi Alton,
    [/qb]<hr></blockquote>

    So as per the above code the sb1 will continue to point to the class level object (denoted by sb) till it explicitly points to new object. So sb1 will just act as an alias to that class level variable, till some new object get assigned to it.
    I was mis-interpreting it as variable shadowing, but it is variable hiding.
    Thanx a lot.
    [ September 18, 2003: Message edited by: Manish Sachdev ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Thread Quest from Marcus Green Exam1
     
    Similar Threads
    doubt in Threads
    Doubt in Marcus Green's mock question on threads
    Thread
    Question 19 from Green's Mock
    a strange question with Thread