How is an object stored in heap?

Bicycle bicycle = new Bicycle(20,10)

The reference bicycle will be store in stack whereas the object and instance variables will be store in heap and the address of the heap is assigned in the stack so mean to say stack will link to heap.


First of all, you should understand the meaning of Object in terms of Java.

Object is nothing but just a buffer(memory area) in Heap. That buffer or memory area is called Object.

Object contains all the non-static data member of the class.

All the-

Object stores in Heap.

Static data member stores in Class Area.

Reference variable stores in Stack.

Method (static or non-static) stores in Method Area.

Memory Area

Read more on Java Memory Model


Want someone else to do your CS homework eh? ;)

Depending on the language the exact implementation will be different (how it's stored in memory), but the general concept is the same.

You have your stack memory and your heap memory, the local variables and parameters go on the stack, and whenever you new something it goes into the heap. It's called a stack because values are pushed onto it when you declare it or call a function, and popped off and they go out of scope.

                    +--------------+
|              |    |              |
|              |    |              |
|              |    |              |
|              |    |              |
|              |    |              |
|              |    |              |
+--------------+    
     Stack                Heap

Each instance variable takes up however much memory its type does (depending on the language), the compiler adds it all up and that's the sizeof the type (à la C++). Methods go into code space and does not get newed into the object (I think for now you'll be better off to not consider this in learning about how memory is organized, just think of it as magic).

So in your example:

Bicycle bicycle = new Bicycle(20,10)
  • bicycle is a reference to the heap memory address, today in most languages/systems it will cost you either 32 and 64 bits on the stack.
  • The new allocates memory in the heap. The compiler figures out the size of Bicycle and creates assembly/machine code that allocates the amount of memory it requires.

This is how the memory looks after this line:

                    +--------------+
|              |    | Bicycle obj  |
|              |    |--------------|
|              |    |              |
|              |    |              |
|--------------|    |              |
| bicycle ref  |    |              |
+--------------+    
     Stack                Heap

More specifically, since the Bicycle class has two instance variables (or fields as they are called in Java) and both are ints, and an int in Java is 32 bits or 4 bytes, the size of your Bicycle object is 4 bytes * 2 fields = 8 bytes.

                   +-------------+
|             |   0| gear        | 
|             |   4| speed       |
|             |    |-------------|
|             |   8|             |
|-------------|  12|             |
| bicycle=0x4 |    |             |
+--------------+    
     Stack                Heap

Time complexity to access memory is O(1). The compiler is able to figure out the exact memory address of speed, since as the second int field in the object, is at bicycle+0x4.