1

I defined a 2d array in Java. As I read about it(i.e. 2d array), the first dimension of this 2d array is a pointer (I do not know that is it right or not, please tell me about it). So If I consider it as pointer, in a 64-bit system, what will be the size of below code after execution?

    short [][] array1 = new short [10][];
    short[][] array2 = new short[10][];
    for (int i = 0; i < 10; i++)
        array1[i] = new short[1];
    for (int i = 0; i < 10; i++)            
            array2[i] = array1[i];         

Please tell me about the size of above code.

1
  • The first index is the number of columns, the second is the number of rows. The first index is required, but the other does not need to be specified at creation time; Through your for loops you're creating 2 10x10 arrays. Commented Nov 11, 2013 at 18:54

1 Answer 1

2

For every one dimensional array you have a 24 byte overhead in addition to the space for the data in the array.

  • So your first two lines of code each create an array of 10 pointers - you are right about that - which take 8 bytes each on 64-bit system. This means you are allocating 2 * (24 + 10 * 8) = 208 bytes.
  • In the first for loop you are creating 10 arrays which are 24 + 2 * 10 = 44 bytes each. These are padded to at least 8 byte boundaries and thus take up 48 bytes or 480 bytes in total.
  • In the second loop, you are not allocating any new memory.
  • In total you are using 208 + 480 = 688 bytes.

Note that the actual usage depends on the JVM. For example:

In order to see the difference between outer and inner arrays it might be helpful to rewrite your code like this:

short[][] outerArray = new short[10][]; // Array of references to short arrays
short[] innerArray;                     // Array of shorts
for (int i = 0; i < 10; i++) {
  innerArray = new short[1];
  outerArray[i] = innerArray;
}
Sign up to request clarification or add additional context in comments.

4 Comments

...but, can you tell me why when I ask the system about the class of first dimension it says me that I am using short? You are saying that the first dimension is pointer, the pointer is not short. What is the problem?
There are no "real" two dimensional arrays in Java. The "first dimension" is a one dimensional array of references/pointers to one dimensional arrays of shorts.
When I run this code : System.out.println(array1[1].getClass().getCanonicalName());, the system returns this: Short []. I am confused, If this is a lie, so here is a bug, if this is true, so we have not any pointer here...
See my edit above. In short everything that has just one pair of braces (e.g. short[]) is an array of that type. Everything that has two pairs of braces (e.g. short[][]) is an array of references/pointers to arrays of that type. Try this to get clarity: System.out.println(array1.getClass().getCanonicalName());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.