• Register
First time here? Checkout the FAQ!
Welcome to Find4Answers.com

Where you can Ask Questions, Find Answers Or Receive Answers from other members of the community And Share in Social networking sites like facebook, linkedin, twitter.
3cx phone system assignment writing service bandar bola best waikiki restaurants boiler business car car insurance quotes car insurance quotes car insurance quotes car lease deals car leasing uk chwilówka przez internet zapewne company convey convey law complaints convey law reviews convey law service conveylaw cosmetic dentist csr classics hack csr classics hack csr classics hack data data recovery maidenhead deals design development double glazing leeds electrician electrician manchester electricians emergency farm filmy bez limitu filmy online finlock finlock solutions general genral golf holidays spain graduate jobs in london graduate jobs london heap how to jump higher how to jump higher how to jump higher infrared sauna infrared sauna saunas inline skating java judi bola law law firm in leeds lease leasing led lights led bulbs leeds legal smoke life insurance quotes log london manchester mercedes lease deals mercedes leasing mezzanine movies museums and art nikogo szybka chwilówka none none none ny male revue outsource link building paid search agency performance car hire personal personal injury lawyer porcelain veneers ramię szybkie chwilówki recovery restaurant seo seo agencies seo company seo company london seo las vegas seo services services sky diamond seo solutions steel synchronization szybka chwilówka przykład szybka chwilówka żaden szybkie chwilówki lekko temp cover car insurance temp cover car insurance temp cover car insurance thread tymczasem chwilówki przez internet upvc windows manchester viagra kamagra videos vinyl flooring suppliers wait web development company window repairs london windows zobaczyć szybka chwilówka

What is stored on Stack and Heap ?

0 votes
Hi all,

Can anyone tell me what is stored on a stack and what is stored on a heap in a Java program?

related to an answer for: What is heap and stack?
asked Nov 12, 2011 in Programming by anonymous

3 Answers

0 votes

AS I Think
Primitives are stored on the stack, objects in the heap space.

int j = 20; will be stored on the stack, because the variable itself holds the value and it's much faster, but if you wrap it:
Integer p = new Integer(20); then it's stored in the heap space because it's now an object.

answered Nov 12, 2011 by support Hot Users (380 points)
@support Well Heap and stack are the two main memories that JVM is concerned as far as memory allocation by the OS(operating system) is concerned. As said above, stack and heap are the memories allocated by the OS to the JVM that runs in the system. Stack is a memory place where the methods and the local variables are stored. Heap is a memory place where the objects and its instance variable are stored. Also it is to be remembered that the variable references (either primitive or object references) are stored in the stack. i have written this to the best of knowledge. Any corrections are welcome. Thank You.
@sarada "Stack is a memory place where the methods and the local variables are stored." By "methods", I am assumming that you mean the callstack -- not the actual code for the methods. "Heap is a memory place where the objects and its instance variable are stored." Yes. "Also it is to be remembered that the variable references (either primitive or object references) are stored in the stack." Only for local variables. For instance and class variables, they are part of the object, hence, on the heap. Finally, Java 6 may throw a monkey wrench into this. Due to "escape analysis" optimization, sometimes Java 6 will allocate objects on the stack. Thanks.
@support Thanks. But can anyone say where Interface variables are stored ? heap or stack?
@sarada Heap, since interfaces only exists as implementations in classes and they are on the heap.
For better understanding i am providing the below example. Remember in Runtime Environment Data, we have another field named "Method Area" which contains Method information and class variables as well In my opinion, the separation between stack and heap can be deemed as following: Stack: local variables (both primitive
0 votes
  • Instance variables and Objects lie on Heap. Remember this is where the state is maintained and when you get memory leaks this is where your profiler helps you to find the allocation of memory.
  • Local variables and methods lie on the Stack. So if we have a main method which calls the go() method which calls the gone() method then the stack from top to bottom would consist of




as soon as gone() has been processed it would be removed from the stack. Any corresponding local variables which are used in gone() would also be removed from the stack. Stack would have references to objects on the Heap.

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 Collar { }
3. class Dog {
4. Collar c; // instance variable
5. String name; // instance variable
7. public static void main(String [] args) {
9. Dog d; // local variable: d
10. d = new Dog();
11. d.go( d ) ;
12. }
13. void go(Dog dog) { // local variable: dog
14. c = new Collar();
15. dog.setName(“Aiko”);
16. }
17. void setName(String dogName) { // local var: dogName
18. name = dogName;
19. // do more stuff
20. }
21. }









■ Line 7—main() is placed on the stack.
■ Line 9—reference variable d is created on the stack, but there’s no Dog object yet.
■ Line 10—a new Dog 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 dog parameter as a local variable.
■ Line 14—a new Collar object is created on the heap, and assigned to Dog’s instance variable.
■ Line 17—setName() is added to the stack, with the dogName 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 Dog 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 dogName disappears too, although the String object it referred to is still on the heap.

answered Nov 16, 2011 by javaboy Hot Users (380 points)
0 votes

When a function or a method calls another function which in turns calls another function etc., the execution of all those functions remains suspended until the very last function returns its value.

This chain of suspended function calls is the stack, because elements in the stack (function calls) depend on each other.

The stack is important to consider in exception handling and thread executions.


The heap is simply the memory used by programs to store variables. Element of the heap (variables) have no dependencies with each other and can always be accessed randomly at any time.
answered Nov 16, 2011 by anonymous

Related questions

0 votes
2 answers 112 views
0 votes
3 answers 363 views
asked Nov 6, 2011 in Computers by sarada Hot Users (2,340 points)
0 votes
1 answer 233 views
0 votes
0 answers 101 views