• Post Reply Bookmark Topic Watch Topic
  • New Topic

Explain the code's output with reference to stack and heap  RSS feed

 
chirayu patil
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



output:
30
20
30
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please always tell us where you got such questions from, so as to confirm there is no copyright problem about them. I have added code tags, which you should always use, and you can see how much better the post looks
You do not need to know anything about the stack and the heap to explain the output.
I suggest you get a piece of paper and you draw squares representing the objects, and arrows pointing to x y z. Then you can write the values of the int in each of the squares, and you will be able to see not only why the output is as it is, but also, I hope, the design errors in class AA.
 
chirayu patil
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh thnxxx...i am new to javaranch , so i didnt knew about using code tags....
and i got this from my friend so no copyright issue... :P
i am interested in knowing wht happens in heap and stack when this code is compiled and executed....
but anyways thnxx for your fast reply
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because of an optimisation in Java6, you cannot tell whether the objects are created on the heap or temporarily on the stack.
You can tell that the Class<AA> and Class<BB> objects are put on the heap, for the instances to be created from. You can tell what the instructions to put onto the stack are, if you read the output from javap -c BB
You can find what the individual instructions mean in the Java Virtual Machine specification.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!