This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Heap and Stack Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Heap and Stack" Watch "Heap and Stack" New topic
Author

Heap and Stack

Ragav Baskaran
Ranch Hand

Joined: Nov 04, 2007
Posts: 36
Hello All,

Can some one explain me the difference between Heap and Stack.

Thanks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

a quick google search on "computer memory heap vs stack" returns many good answers to that very question.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
Stack in java is used to store methods and local variables or stack variable,Automatic variable.
It is useful in threading and exception handling. It is LIFO Last In First Out . Elements in the stack depends on each other ie for example
void chain()
{
int a;
check();
} here while running program first chain() method will get invoke .after that check() will be called..now check() will be in the top of stack memory.after the execution of check() only chain() will get finish (that is why LIFO) its execution.(If you are a beginner then my humble request to read Head First Java)

On the other hand Heap is mainly used to store Objects.whatever objects you are creating that thing is stored in Heap only.All instance variables are stored in heap only.

hope this will help you


Anto Telvin Mathew<br />Many of the life failures are people who did not realize how close they were to success when they give up. EDISON
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Heap and Stack
 
Similar Threads
Where do static variables (class variables) live (scope)?
JVM memory management
Where do primitive types live?
Heap / Stack
When will a variable use memory from stack and when will use memory from heap?