aspose file tools*
The moose likes Beginning Java and the fly likes Stack Vs Heap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Stack Vs Heap" Watch "Stack Vs Heap" New topic
Author

Stack Vs Heap

Prashant Sehgal
Ranch Hand

Joined: Jun 20, 2003
Posts: 56
Hi,
I hope I am not falling too much out of line here, but coming from a programming background involving both C++ and Java, my question is about how Stack and Heap are treated differently in both languages.
I read in a post somewhere that...
".... Java programmers moving to C++. Since in Java all your classes are created with "new" I doubt Java programmers come to C++ with much of an idea of the difference between stack and heap allocation and what should go where (I often wondered how Java could get away with this until I realized that Java uses a different heap structure... Doh.)..."
Firstly, can anyone explain the exact difference between Stack and Heap? Also, can anyone please explain what sort of a "heap-structure" is used in Java?
Thanks.
James Clinton
Ranch Hand

Joined: Jun 23, 2003
Posts: 190
I can part answer this question.
The heap is used for objects that are created dynamically at run-time. All objects in Java are like this as they are created using the new key word.
//C++ has both.
Object o = new Object() //c++ or java dynalic obj
//C++ object on stack
Object o; // if used in java this would result in a null pointer, but is valid in C++.

Dynamic objects in java are therefore used and then removed when null by the garbo collector. C++ coders would specifically have to "delete" the object to avoid memory leeks.
The stack is used for objects that the computer knows the size of, I *think* all static types in Java are stored there. (behind the scenes they are essentially memery addresses with pointers attached to them, java programers are shielded from this thou).
Pls correct me if I'm wrong.
/James
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Prashant, Welcome to JavaRanch!
I seem to recall seeing this topic discussed a few times recently, and in the more distant past. Try a quick search on this forum, and on the Intermediate forum. Note that the search page link is at the top right of this page.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
peter greaves
Ranch Hand

Joined: Sep 27, 2002
Posts: 51
>The stack is used for objects that the computer knows the size of, I *think* all static types in Java are stored there
james - did you mean, "objects that the COMPILER knows the size of, " ??

peter


SJCP 1.2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stack Vs Heap