Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stack

 
Arpana Rai
Ranch Hand
Posts: 93
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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??
regds
Arpana
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic