Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an array of references

Tags:

java

If I have a class say A, and I declare an array of 10 elements of this class as,

A [] arr=new A[10];

Then 10 new objects of A are created and stored in the array.

However, i'd like to be able to do something on the lines of A arr[10]; , where the array just holds references to null objects.

The reason I need this is because I only need the array to hold instances I fill in later in the code. So the objects the above statement creates are lost anyway and as I understand object creation is expensive.

So, is there any way to just have an array of references that I can point to the objects I desire later? Or is this not possible and I should resort to using an ArrayList?

like image 249
Akash Avatar asked Feb 25 '13 10:02

Akash


People also ask

Can you make an array of references?

An array of references is illegal because a reference is not an object. According to the C++ standard, an object is a region of storage, and it is not specified if a reference needs storage (Standard §11.3. 2/4). Thus, sizeof does not return the size of a reference, but the size of the referred object.

How do you create an array of references in Java?

To use new to allocate an array, you must specify the type and number of elements to allocate. Note : The elements in the array allocated by new will automatically be initialized to zero (for numeric types), false (for boolean), or null (for reference types). Do refer to default array values in Java.

Can we create array of reference in Java?

Array references can be used anywhere a reference to type Object is called for, and any method of Object can be invoked on an array. Yet, in the Java virtual machine, arrays are handled with special bytecodes. As with any other object, arrays cannot be declared as local variables; only array references can.


3 Answers

If I have a class say A, and I declare an array of 10 elements of this class as,

A [] arr=new A[10];

Then 10 new objects of A are created and stored in the array.

That's not correct. Here, an array of ten references gets created, and each reference gets set to null. There are no instances of A created by this code.

In other words, the code already does exactly what you'd like it to do.

like image 181
NPE Avatar answered Oct 28 '22 00:10

NPE


if you do A [] arr=new A[10]; then no objects are created except your array, each field will be null until you initialize it.

A [] arr=new A[10]; only create place to store reference of A Class object. Although array arr is created but its not referencing any object and you can't do like arr[i].someMethod().

To correct it, allocate object at individual memory in array do like this:

A [] arr=new A[10];
arr[0] = new A();
arr[1] = new A();
:
:

or in a loop like:

  for(i=0; i<10; i++){
      arr[i] = new A();
  }

After there arr that is an array of reference, refers to a valid A class object. And after this expression arr[i].someMethod() will not cause an error.

like image 36
Peter Avatar answered Oct 28 '22 00:10

Peter


From JLS Kinds of Variables:

Array components are unnamed variables that are created and initialized to default values (§4.12.5) whenever a new object that is an array is created (§10, §15.10). The array components effectively cease to exist when the array is no longer referenced.

And from 4.12.5. Initial Values of Variables

For all reference types (§4.3), the default value is null.

So as you says:

If I have a class say A, and I declare an array of 10 elements of this class as,
A [] arr=new A[10];
Then 10 new objects of A are created and stored in the array.

That's not correct. But what you want is correct .

So, is there any way to just have an array of references that I can point to the objects I desire later?

like image 20
Sumit Singh Avatar answered Oct 27 '22 23:10

Sumit Singh