aspose file tools*
The moose likes Beginning Java and the fly likes how to distinguish the stack and the heap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to distinguish the stack and the heap" Watch "how to distinguish the stack and the heap" New topic
Author

how to distinguish the stack and the heap

nick magic
Greenhorn

Joined: Mar 03, 2006
Posts: 11
the variables are always on the stack , not the heap
how to comprehend this sentence, and what is the difference between stack and the heap?
thanks!


la qualite de la vie!
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

A little article which says it all:
http://www.informit.com/articles/article.asp?p=31755&seqNum=8&rl=1


[My Blog]
All roads lead to JavaRanch
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Here's some additional information and background from Thinking in Java (3rd ed).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
This subject is not on the exam syllabus


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi nick ,

Basically, stack is a memory space reserved for variables of primitive types declared inside a METHOD. Everything that's different from this, will be created on the heap.

For example, outside a method, if you create an instance variable for a primitive or a reference variable for another object it will be created on heap.

On the other hand, if you create within a method a variable for a primitive, it will be created on stack, however, if you create a reference variable within a method, it will be created on the heap.

In summary, reference variables are always created on the heap, regardless whether or not they are inside a method.

And only variables created for primitives within a method are created on the stack.


"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
tapeshwar sharma
Ranch Hand

Joined: Mar 10, 2006
Posts: 245
I think the answer to this has been posted on this site only albeit in a different section.
The answers lies in predictability of the lifetime of a variable.
For example, all variables declared within a method have a predictable lifetime(they will exist as long as the method finishes execution).
Hence they are stored in a stack.So, a stack is a memory management structure used to store "predictable lifetime" variables.References to objects, and variables declared within the methods are stored in a stack.

All instance variables and objects have unpredictable lifetime, so they are stored in the heap.I think drawing an anology with the literal meaning of stack and heap might help you to understand this concept.

thank you
tapeshwar sharma
Ranch Hand

Joined: Mar 10, 2006
Posts: 245
Btw, I am only quoting someone else.
I don't deserve any credit other than seeking to help you out :-)
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi parshub,

Concerning your affirmation :


For example, all variables declared within a method have a predictable lifetime(they will exist as long as the method finishes execution).
Hence they are stored in a stack.

References to objects, and variables declared within the methods are stored in a stack.



I think your affirmation is not exactly correct, think about objects declared within a method. Even after the method execution finishes, the object created within a method still remains alive probably waiting for garbage colletion. So, its life time is unpredctable. That's why Java puts all objects within heap.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"parshub,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your name here.

Thank you for your prompt attention, and enjoy the ranch!

-Marc
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Marcus Green:
This subject is not on the exam syllabus

Good point. I'm moving this to the Java In General (Beginners) forum...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Edisandro Bessa:
In summary, reference variables are always created on the heap, regardless whether or not they are inside a method.


Sorry, but this is wrong. All objects are created on the heap; this includes, of course, the contents of all objects (their member variables). But local variables, regardless of their type, are always on the stack. The object a local reference variable refers to is, like all objects, on the heap; but local variables of every type, reference or primitive, are on the stack.

As to your correction of someone's correction: that's really not right either. When a variable goes out of scope, it's gone. It doesn't matter what kind of variable it is. This is quite apart from the lifetime of any object a variable might have referred to. As soon as an object is unreachable, it's gone. The JVM may grind it to a pulp instantly, or it may never collect it. But just the same, it's gone to your program, and that's perfectly determinate.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to distinguish the stack and the heap