File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes how primitives and objects are laid in memory? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how primitives and objects are laid in memory?" Watch "how primitives and objects are laid in memory?" New topic

how primitives and objects are laid in memory?

Dez Max
Ranch Hand

Joined: Oct 01, 2002
Posts: 41
Hi Folks,
I want to know how java objects and primitives objects are laid out in memory. How static variables and objects are stored. For ex
int a = 12;
how memory allocation happens for variable a.
Thanks in advance.
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at
We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please edit your profile and select a new name which meets the requirements.
Please be sure to change your display name soon. Accounts with invalid names get deleted.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Objects are stored on the heap. Static variables are stored with the Classfile. Variable references are stored in the stack related to the Frame. There is a frame for each method invoked. So local variables find themselves on the stack for the method that they are local to.
The example that you gave shows the integer as a constant value. That value would be stored in the Constant Pool, with all the other constants and literals and Strings.
You may enjoy reading the Java Virtual Machine Specifications for details on how such things work. .

"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link:
subject: how primitives and objects are laid in memory?
It's not a secret anymore!