File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Heap and Stack difference. 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 "Heap and Stack difference." Watch "Heap and Stack difference." New topic
Author

Heap and Stack difference.

Kosh Shah
Greenhorn

Joined: Jul 20, 2004
Posts: 19
Hi friends,

"Space for objects created with the new operator are allocated in the heap. Method arguments or local variables of primitive types are created on the stack. Objects created in the heap have a longer lifetime.
Objects are kept on the heap, and since objects can contain instance variables of primitive types, these primitive variables are kept on the heap as well."

Can anyone throw more light on the above? I seem to have mixed up my understanding in the above statements.

Cheers,
Kosh
Chandra Sekhar
Ranch Hand

Joined: Sep 26, 2003
Posts: 90
Hi,

Primitive data types have just one value to store. For instance:

int i = 1;

The appropriate amount of space is allocated given the data type, and the variable is stored in memory just as it is.

Objects must be stored differently because they are more complex. They often hold multiple values, each of which must be stored in memory. The association between each value and the object must be maintained throughout its life. An object reference variable must then hold a reference to those values. This reference represents the location where the object and its metadata are stored.

There are two kinds of memory used in Java. These are called stack memory and heap memory. Stack memory stores primitive types and the addresses of objects. The object values are stored in heap memory. An object reference on the stack is only an address that refers to the place in heap memory where that object is kept.

Say you've got two Test objects, and you assign the first to the second, like this:

Test test1 = new Test();
Test test2 = new Test();

test2 = test1;

What you're actually doing when you write this is assigning the address of the test1 object to the test2 object. Assume that test1's memory address was 0x33d444 and that test2's address was 0x99f775. After performing the above assignment, test2 now holds this address in stack memory: 0x99f775, which refers to the same object as test1. The test2 object on the heap still exists, but it cannot be accessed. That's because this reassignment overwrote the old address that test2 was keeping on the stack. This kind of reassignment makes two stack references to the same object on the heap.

It is useful to know that these two different kinds of memory exist in Java. Stack memory is the program's memory, and heap memory resides outside of the program.

As a Java programmer, you do not have to directly address memory allocation and recovery of memory space, which is a common headache for C++ programmers. When you need a new object, Java allocates the required memory. When you are done with an object, the memory is reclaimed for you automatically via Java's garbage collection facility.


Refer

Chandrasekhar
SCJP

[ September 30, 2004: Message edited by: Chandra Sekhar ]

([QU0TE][/QU0TE] tags added - see link for source)
[ October 01, 2004: Message edited by: Barry Gaunt ]

"Luck is when opportunity meets preparation"
Kosh Shah
Greenhorn

Joined: Jul 20, 2004
Posts: 19
Awesome!

That was helpful. And hence, better able to correlate it to the Garbage Collection as explained in your link.

I would also reccomend reading Corey's blog with regards to GC, the one which is titled as "A pic is worth a 1000 words...".

Cheers,
Kosh!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Heap and Stack difference.
 
Similar Threads
gc from msterexam
aggregate initialization
Java Heap and Stack
how to distinguish the stack and the heap
static variables - kept on Heap or Stack