• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

push method......

 
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
 
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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
 
He baked a muffin that stole my car! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic