GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes differenciate 'heap' and 'stack'   Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "differenciate Watch "differenciate New topic
Author

differenciate 'heap' and 'stack'

Rajiv Goyal
Greenhorn

Joined: Jul 28, 2003
Posts: 25
hi,
1)what is difference between the two ie heap and stack in java terminology
2)Can someone summarize as different collections (like treest,linkedlist etc )are used/useful for under what circumstances
thanks,
Rajiv
[ August 25, 2003: Message edited by: Rajiv Goyal ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
The heap is the dynamic chunk of memory where the objects are created. Java programmers have no pointers to acces the memory directly. The garbage collector frees memory automatically for unreferenced objects in the heap.
A Java thread is given a program counter and a Java stack. A thread cannot access the stack of another thread. When the thread calls a method a stack frame is created for the method and pushed in the thread's stack. The frame becames the current frame. Upon method completion, either normally, or abruptaly --throwing an exception-- the current stack frame is popped off the stack and discarded. The previous stack frame becomes the current one.
A frame stack has three sections:
A) local variables. For storing the local variables and parametes
B) operand stack. For storing the intermidiate result of calculations. There are no registers in the JVM!
c) frame data. For accessing the constant pool of the class where the method was declared, normal return of the method etc.


SCJP2. Please Indent your code using UBB Code
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
The constant pool sounds like a set of virtual registers.
Does Java have a limit to the variable stack size? [When writing for an embedded system, whether using C or assembler, the programmer usually has to set the stack size (unless the stack is a reserved part of RAM, like in the old 6502 processor).] Can the programmer set the stack size, or does it just automatically grow as long as assignable memory is available?


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hello Jeff.
from API:

StackOverflowError
extends VirtualMachineError
Thrown when a stack overflow occurs because an application recurses too deeply.

Though I have never use it, from java launcher documentation:

-Xssn
Set thread stack size

The compiler is able to compute the size of the local variable and operand stack sections for every method. The size of frame data is fixed. Still, if too many invocations occurr we will see a StackOverflowError
[ August 26, 2003: Message edited by: Jose Botella ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I think of the contant pools as the firmware of the JVM: though the JVM resolves the entries in the constant pool translating symbolic references to direct ones, a Java program only reads from such entries, it will not write to them as it would do with registers.
In fact the JVM arquitecture is stack oriented, it uses the operand stack for storing intermidiate results.
Dhanashree Mankar
Ranch Hand

Joined: Aug 25, 2003
Posts: 123
Originally posted by Rajiv Goyal:
hi,
1)what is difference between the two ie heap and stack in java terminology
2)Can someone summarize as different collections (like treest,linkedlist etc )are used/useful for under what circumstances
thanks,
Rajiv
[ August 25, 2003: Message edited by: Rajiv Goyal ]

set: stores unique elements but not ordered.
linkedhashset: they r ordered and unique.It doesn't delete least recently used element when new is added.
Implementation of set is hashset.
list: elements are in ordered but not unique. They can contain duplicates.
Implementation of list are Arraylist,linkedlist and vector.
vector is thread safe but arraylist is not.
when searching some element arraylist and vector r giving good performance. but linkedlist not since we have to traverse each element.
But at the time of insertion and deletion linked list is preferred.
map: contains the elements in key value pair.
Implementations are hashmap and hashtable.
hashmap is not thread safe and can contain null elements.
hashtable is thread safe and can not contain null elements.
linkedhashmap : It deletes least recently used element when new is added.
and can contain nulls. elements i.e.keys are in order.
Rajiv Goyal
Greenhorn

Joined: Jul 28, 2003
Posts: 25
Thanks all; that answer my quesion,
Rajiv
[ August 26, 2003: Message edited by: Rajiv Goyal ]
 
GeeCON Prague 2014
 
subject: differenciate 'heap' and 'stack'