• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread Quest from Marcus Green Exam1

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pls help ...
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
M Sharma
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 443
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 106
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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 ]
     
    reply
      Bookmark Topic Watch Topic
    • New Topic