• Post Reply Bookmark Topic Watch Topic
  • New Topic

push method......  RSS feed

 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The anwer is :
[100] []
Can any one explain me how do we get the above answer in this problem?
Sonir
 
Shivaji Marathe
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sonir :
This question is a classic example of the passing arguments to a method and the result the method has on the original object.
A copy of the reference of the stacks is sent to the method. Think that S1 and S2 containnot the stack itself but some address where the actual stacks reside.
Within the method X1 and X2 contain the same addresses as S1 and S2. that is X1 is a copy of S1 and X2 is a copy of S2.
x1.push (new Integer ("100"));
Using that reference, the first statement modifieds one stack.
x2 = x1;
This second statement simply changes X2 so that it now points to the same obejct that X1 points to.
So after this method is executed, the object pointed to by S1 has changed, while the object pointed to by S2 has not changed at all.

If you draw all this on a piece of paper, similar to the diagrams in the RHE book , it will be much clearer to you.
Hope this helps
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this post too:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014192
HIH
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!