This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Stack Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Stack" Watch "Stack" New topic


Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93

The output of above code is:
st1 has: []
st2 has: [100]
I thought the output would be:
st1 has: [100]
st2 has: [100]

Plz some one explain me why the above code gives such output??

Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Inside method, the parameters st1 and st2 are not the same as the local variables st1 and st2 in the main method. Think of them as st1' and st2' . After the invokation they point to the same Stack objects as the local variables in main do. Therefore, modifying the content of st2' will also be reflected in the variable st2 in the main method. However st1'= st2' will not set st1 to st2' , because st1 and st1' are different variables. st1' dissapears after the method invokation returns, but st1 remains unchanged.

SCJP2. Please Indent your code using UBB Code
jQuery in Action, 2nd edition
subject: Stack
Similar Threads
How about with java.util.Stack??
Stack referencing stack
Static Block
Stack referencing other stack
Doubt in StirngBuffer