179k views
3 votes
If you declare an array as "int[] a;", what do you have to re-do when you instantiate it?

1 Answer

7 votes

Final answer:

To instantiate an array declared as 'int[] a;', you need to use the new operator to allocate memory and specify the array's size, such as 'a = new int[10];', which creates an array of ten integers.

Step-by-step explanation:

When you declare an array in Java as int[] a;, you essentially create a reference variable that can point to an array of integers. However, at this point, a does not refer to any array. To instantiate (or create) the array, you must specify the size of the array and allocate memory for it. This is done with the new operator, which initializes the array with default values (0 for int). For example, to instantiate an array of ten integers, you would write a = new int[10];. After instantiation, you can access and assign values to elements of the array using their index.

User Ziya ERKOC
by
8.7k points