• 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

Questions like these confuse me

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please can somebody explain me the flow

1) 10030
2) 20030
3) 209930
4) 10020
------------------
coffee drinker
[This message has been edited by amit mawkin (edited November 20, 2001).]
I fixed the [ code] tags for easy viewing -- Carl
[This message has been edited by Carl Trusiak (edited November 20, 2001).]
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is 4.
In method another(...){...}
it sets i = 0 and v.i = 20.
And again reset v = another ValHold object with i = 10;
So the output in this method is 10 and then 0.
After this method in method amethod(){...}
The output would be 20 becuase in method another{} set it to v.i = 20;
So, all the output together would be 10020.

Originally posted by amit mawkin:
[B]please can somebody explain me the flow

1) 10030
2) 20030
3) 209930
4) 10020
[/B]


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

Originally posted by amit mawkin:
[B]please can somebody explain me the flow

1) 10030
2) 20030
3) 209930
4) 10020
[/B]



The answer is 4. I have added comment code above to explain what is going on.
Matthew Phillips
 
amit mawkin
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys especially the line by line analysis was cool
 
amit mawkin
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matthew
one last doubt the value of i passed to another is the local value correct me if ia m wrong.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First thing to remember, variables are passed by value, which means that you cannot assign a new value to the variable in the caller method. Passing a reference variable by value means that a cipy of the reference is passed and thus you can only interact with the referenced object by manipulating it through its methods and member variables but you cannot make the reference point to another object inside the callee.
Now, the flow is the following:
We begin in ObParm.main.
An ObParm object is created and referenced by variable o of type ObParm. Then, method amethod() is invoked on o.
Method amethod() does the following:
Creates and init a new integer variable i with value 99.
Creates and init a new ValHold object referenced by variable v of type ValHold. (v.i is now 10)
Changes the value of v.i to 30. (v.i is now 30)
Invokes another() on this ObParm object (i.e. object referenced by the variable o)
The invocation occurs with following arguments: v which is the ValHold object that we just created and i which is the variable i created at the beginning of this method and whose value is 99. So the invocation is another(v,99). Note that a copy of the reference v is provided in fact and not the actual variable v due to pass-by-value rules of Java.
We are now inside the method another:
the first statement (i=0) affects 0 to the parameter i but JUST in the scope of this method, the change does not affect the i in method amethod ! This is very important.
We change the value i of the object referenced by variable v to 20. (v.i is now 20).
We create a new valHold object referenced by variable vh of type ValHold.
We make the parameter v reference vh. Again this only affects the scope of this method and does not change the referenced object in the caller method (amethod) due to pass-by-value rules.
We print out the member i of v which is the member i of vh that we just created and whose value is 10.
Then we print i which is the value of the parameter i of this method. i was passed with value 99 but has been changed on the first line to contain 0. So we print 0.
The control returns to amethod and it prints the value of the member i of the object ValHold referenced by v. Remember that we changed that value when we executed v.i=20 in another. So we print 20.
Summing up, the answer is 10020, so the fourth one.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Fei Ng
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by amit mawkin:
Matthew
one last doubt the value of i passed to another is the local value correct me if ia m wrong.


Yes, i is a local value in method amethod().
a copy of i is passed to another().
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi amit,
The key to this question is recognizing that

  • reference variables are passed by value
  • the 'value' of a reference variable is the memory address of the object it references
  • a method can change the original object but not the reference to the object

  • Keeping all that in mind, walk through the code to see what will be displayed.
    In <code>amethod</code> the statement <code>ValHold v = new ValHold();</code> creates a new <code>ValHold</code> object with 'i' equal to 10.
    The next statement sets the value of 'i' to '30' then the object is passed to <code>another()</code>.
    The statement <code>v.i = 20</code> writes over the '30' assigned in the previous method (remember that what gets passed to a method is the 'memory address' of an object, so the reference variable 'v' in 'another()' still points to the original object.)
    Then a new <code>ValHold</code> object is created and it's variable 'i' is '10'.
    In the next statement, the new ValHold object is assigned to the parameter variable 'v'. In the method, the parameter variable is now referencing a completely new object, not the orignal. The <code>System.out.println</code> statement displays '10' which is the value of 'i' in the new object.
    The next statement just prints out the value of the local parameter variable 'i' which has been set to '0'.
    Returning to <code>amethod()</code> the value '20' is printed. The orginal object was changed in <code>another()</code> but the reference to the object was not. The reference variable 'v' in <code>amethod</code> continues to point to the original object.
    Hope that helps.
    ------------------
    Jane Griscti
    Sun Certified Programmer for the Java� 2 Platform
    Co-author Mike Meyers' Java 2 Certification Passport
 
amit mawkin
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks to all
I think now after reading through all explanations I will not get confused.
regards
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading over the explanation of this code and how it works, and I have a quick question. Why doesn't the line v = vh change the reference of v. v is an object, so it is passed by reference and if you change that reference in the method, its reference is also changed in the calling method isnt't it? If that is the case then when v.i is output in amethod, the result would be 10 or the same value that was in vh which v now references, or am I totally missing something? So with my logic which is appearantly wrong I would assume the final output to be 10010. So what am I missing here? I may need some serious explanation, because it appears that I am confused.
[This message has been edited by Matt Wheeler (edited November 20, 2001).]
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matt,
even references are passed by value. In fact a copy of the reference is passed to the method. As Jane pointed out,


reference variables are passed by value
the 'value' of a reference variable is the memory address of the object it references
a method can change the original object but not the reference to the object


There is no such thing as passing by reference in Java.
There is a good explanation at:
http://www.javaranch.com/campfire/StoryPassBy.jsp
http://www-106.ibm.com/developerworks/library/praxis/pr1.html?dwzone=java

HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited November 20, 2001).]
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Matt Wheeler:
. . . its reference is also changed in the calling method isnt't it? <snipped for brevety>


No. The current method only has a copy of the reference. When you change the reference of v to vh in another(), it does not affect v in amethod.
Matthew Phillips
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for such a detail explanation.
Some of the imp thinks to remember is that in Java variable cannot be pass by reference.
But a variable is PASS BY REFEREBCE BY VALUE , where a copy of the variable is created
CHEERS.
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by MONZY THARIAN:
Thanks to all for such a detail explanation.
Some of the imp thinks to remember is that in Java variable cannot be pass by reference.
But a variable is PASS BY REFEREBCE BY VALUE , where a copy of the variable is created
CHEERS.


Point is that ... in java everything is passed by VALUE only.
but as Reference variable reference the memory, hence they can change the content at that memory. but it self if it is changed then that change won't be seen outside method.
I mean
<pre>
SomeClass obj = new SomeClass;
someMethod(obl);
someMethod(SomeClass sc){
sc.i = 15; // value of actual parameter will change i.e obj.i will change
sc = new SomeClass(); // nothing will happen outside the method
sc.i = 30; // it is local to mathod
}
</pre>
CMIW
------------------
Regards
Ravish
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am I getting older than you folks? I cannot read the code in the
1) 10030
2) 20030
3) 209930
4) 10020
[/B]
reply
    Bookmark Topic Watch Topic
  • New Topic