2

I have few basic questions on arrays.

  1. Consider I declare an array of integers

    int intArray[] = new intArray[10];
    intArray[0] = 10;
    intArray[1] = 20;
    // and so on...
    

How is intArray stored in memory (I mean the elements of the array)?

  1. Consider I declare an array of objects of some type, say Employee

    class Employee {
        int x = 10; 
        int y = 20; 
    
        Employee(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
    
    class TestEmployee {
        public void main(String args[]) {
            Employee empArray = new Employee[10];
            empArray[0] = new Employee(10, 20);
            empArray[1] = new Employee(30, 40);
            ...
        }
    }
    

What does empArray look like in memory?

1
  • Java is not a good programming language to use and know exactly what's happening on the heap. It actually does its best to take that responsibility away from the programmer. Java manages memory on your behalf, e.g. garbage collections, heap allocation etc. If you want to understand what happens "closer to the metal" I suggest you go with C or C++. Commented Apr 11, 2015 at 18:17

2 Answers 2

3

Consider the following images. The first image shows how an array of primitive types (byte, short, int, long, float, double, char, boolean) is stored in memory.

The array itself being an object is instanciated using the new keyword and is allocated some memory in the heap. But since it is a primitve types arrays it directly stores as much primitives values as its size. These values are set to 0 (in the case of int primitive type array) unless we explicitely change them using an instruction such as intArray[0] = 10;

primitive types arrays

In the case of a refence type array however, there is an interesting difference. The array itself is allocated some memory space on the heap after being instanciated using the new keyword. But it does not directly store the objects but it store the reference to the objects it is holding. These reference would be null or the reference (or adress) to the corresponding object somewhere in memory. The reference to each object has the default value of null unless it is changed with an instruction similar to empArray[0] = new Employee(10, 20);

Regarding your follow up question, in a linked list, suppose I am trying to find an object located at, let's say the 11th position starting from the head of the list, I will have to iterate 10 times to find it. thus the size and the position of an element in a linkedlist will impact the time it takes to retrieve an element. In an array however I simply need to know the index and it finds the element in a constant time.

reference types array

Sign up to request clarification or add additional context in comments.

Comments

1
  1. An array of int: there will be a block of memory which is 10 times the size of an int, in which the values are stored.

  2. An array of Employee: there will be a block of memory which is 10 times the size of an object reference, and each element will refer to an Employee object that is somewhere else in memory (or it will be null).

3 Comments

I have a follow up question on that. Considering empArray will have the reference of the emp objects created some where in the memory. In order to retrieve an object from the array, say empArray[1] will be done in 2 steps. 1. Check the reference address present in the empArray[1] location
I have a follow up question on that. Please correct me if i'm wrong. Considering empArray will have the reference of the emp objects created some where in the memory. In order to retrieve an object from the array, say empArray[1] will be done in 2 steps. 1. Check the reference address present in the empArray[1] location. 2. Go to the address and get the actual object. How is it different from linked list? I feel both array and linked list, element retrieval is same and how are we telling object retrival in array is fast?
A linked list is not organized in the same way as an array. In a linked list, each element contains a reference to the next element. In order to find element number N in a linked list, you'll have to follow all the links starting from the first, to the second, the third, ... until you reach element N. In an array you can directly get element number N because you know that element number N is at position N * size of element from the start of the array.

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.