8

I am doing some studying and I came across a question that asks to show the correct memory diagram of the following code:

int [] d1 = new int[5];
d1[0] = 3;

Integer [] d2 = new Integer[5];
d2[0] = new Integer(3);

ArrayList d3 = new ArrayList();
d3.add(3);

Here is my attempt at a memory diagram, but it may be incorrect:

enter image description here

I understand things like objects, instance variables, and "new" instances are all on the heap and things such as local variables and primitive types are on the stack, but I'm still confused when it comes to array types.

Any help is appreciated.

trincot
  • 317,000
  • 35
  • 244
  • 286
blutuu
  • 590
  • 1
  • 5
  • 20
  • An array is in the end an `Object` (even an array of primitives). By the way, you're close to the final answer. – Luiggi Mendoza May 16 '13 at 04:50
  • @LuiggiMendoza I've changed my diagram. Would this be the correct answer? – blutuu May 16 '13 at 05:01
  • 2
    Almost, the JVM maintains a pool for `Integer` objects from -128 to 127. So the `Integer` with value 3 will be the same for both the `d2` array and `d3` ArrayList. – Luiggi Mendoza May 16 '13 at 05:03
  • @LuiggiMendoza So should the first index of the d3 ArrayList be pointing to the block that d2[0] is pointing to? – blutuu May 16 '13 at 05:16

3 Answers3

5

Any Object on Java lives on heap.

In Java Array is also an Object and hence array Object lives on heap.

Explaination:-

When you write

int a=new int[5],

the (new int[5]) part creates object and hence lives on heap.

Integer x=new Integer(10000)

is also an Object(remember new Operator will always create new Object).

and hence when you wright,

Integer [] d2 = new Integer[5];

it is Array of Integer Object.

As far as ArrayList is considered it is also a class but it wraps array Object and adds dynamic memory to it. So,

ArrayList d3 = new ArrayList();

again creates Object and hence live on heap.

Consider ArrayList class as:

class ArrayList{
    int index=0;
    Object[] obj=new Object['some integer value (depends on JVM)'];
    public void add(Object o){
        obj[index]=o;
        index++;
    }
    //other methods
}

so when you write d3.add(5) actually d3.add(new Integer(5)) is being called.

Remember one golden rule: In java whatever Object you create live on HEAP and their reference live on stack.

Proof of array being object:-

int[] a={4,3,1,2};
System.out.println(a instanceof Object);

//prints true

WebServer
  • 1,316
  • 8
  • 12
  • Nice. Thanks for the explanation. I feel like I have a correct diagram now. Do you see anything wrong with it? – blutuu May 16 '13 at 05:15
0

Arrays are not primitive in java it has concrete class in java.

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created.

System.out.print(int[].class.toString());

So when you create Object of any array type it must go to you heap space.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

Here is an alternate, correct memory diagram.

blutuu
  • 590
  • 1
  • 5
  • 20
  • this memory diagram is still incorrect, so if at all it would be d1[0] and d3[0] pointing to same '3' in the heap however d2[0] and d3[0] will never point to same '3' – sactiw Jan 06 '15 at 12:34