You might be wondering, is Stack and Heap such a large topic that I have dedicated one full chapter to it? Actually it isnt such a big topic but it is extremely important to understand them fully and clearly in order to grasp the forthcoming topics clearly. And combining this with any other topic would make it a bit confusing or rather off-place. So it is given a separate topic status. (We will be touching about these as and when required in the forthcoming chapters and this chapter would just be a quick overview)
For the most part, the various pieces (methods, variables, and objects) of Java programs live in one of two places in memory: either the stack or the heap. For now, let us worry about only three types of things: instance variables, local variables, and objects:
• Instance variables and objects live on the heap.
• Local variables live on the stack.
Let’s take a look at a Java program, and how its various pieces are created and map into the stack and the heap:
1. class Maine { }
2.
3. class Lion {
4. Maine c; // instance variable
5. String name; // instance variable
6.
7. public static void main(String [] args) {
8.
9. Lion d; // local variable: d
10. d = new Lion();
11. d.go(d);
12. }
13. void go(Lion Lion) { // local variable: Lion
14. c = new Maine();
15. Lion.setName("Aiko");
16. }
17. void setName(String LionName) { // local var: LionName
18. name = LionName;
19. // do more stuff
20. }
21. }
This is how the variables and methods get placed in the stack and heap during execution of the above piece of code.
• Line 7—main() is placed on the stack.
• Line 9—reference variable d is created on the stack, but there’s no Lion object yet.
• Line 10—a new Lion object is created and is assigned to the d reference variable.
• Line 11—a copy of the reference variable d is passed to the go() method.
• Line 13—the go() method is placed on the stack, with the Lion parameter as a local variable.
• Line 14—a new Maine object is created on the heap, and assigned to Lion’s instance variable.
• Line 17—setName() is added to the stack, with the LionName parameter as its local variable.
• Line 18—the name instance variable now also refers to the String object.
• Notice that two different local variables refer to the same Lion object.
• Notice that one local variable and one instance variable both refer to the same String Aiko.
• After Line 19 completes, setName() completes and is removed from the stack. At this point the local variable LionName disappears too, although the String object it referred to is still on the heap.
Previous Chapter: Self Test - Chapters 6 to 14
Next Chapter: Chapter 16: Literals, Assignments and Variables
Topics Covered in the Blog - Synopsis
Showing posts with label java stack. Show all posts
Showing posts with label java stack. Show all posts
Friday, January 28, 2011
Chapter 15: Stack and Heap
Labels:
heap,
heaps,
Java,
java heap,
java stack,
SCJP,
SCJP Certification,
stack,
stacks,
stacks and heaps
| Reactions: |
Subscribe to:
Posts (Atom)
© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.
