What does the new operator do when creating an array in java?

When we create the classtype object, the new statement allocates memory at runtime.

Tell me

myclass obj1 = new myclass(); 

here myclass() defines the construction of myclass

but

 int arr1[4] = new int[]; 

new allocates memory, but what does int[] do here?

+4
source share
5 answers

Your code:

 int arr1[4] = new int[]; 

will not compile. It should be:

 int arr1[] = new int[4]; 

putting [] before the array name is considered good practice, so you should do:

 int[] arr1 = new int[4]; 

in general, an array is created as:

 type[] arrayName = new type[size]; 

The part above [size] determines the size of the selected array.

And why do we use new when creating an array?

Because arrays in Java are objects. The name of the array arrayName in the above example is not an actual array, but just a reference. The new operator creates an array on the heap and returns a reference to the newly created array object, which is then assigned to arrayName .

+3
source

The new one allocates memory, but what does int [] do here?

The int[] simply indicates what type the new allocated memory should have.

However, since the array cannot grow dynamically, it makes no sense to write new int[] (even if you specified int[4] in the declaration), you will have to write it just like this

 int arr1[] = new int[4]; 

or how is it

 int arr1[] = {1, 2, 3, 4}; 

Relevant sections in JLS:

+6
source

The above program declares anArray the following line of code:

int[] anArray; // declares an array of integers

Like declarations for variables of other types, an array declaration has two components: an array type and an array name. An array type is written as type [], where type is the data type of the contained elements; square brackets are special characters that indicate that this variable contains an array. The size of the array is not part of its type (therefore, the brackets are empty). As with variables of other types, the declaration does not actually create an array - it simply tells the compiler that this variable will contain an array of the specified type.

+1
source

Creates an instance of an object. new allocates memory on heap . int arr1[4]=new int[]; allocates 14 bytes to the heap.

The correct declaration of the int array is as follows:

 int[] arr = new int[] {1,2,3}; // legal, array of size 3 int[] a = new int[100]; // Declare and allocate, array of size 100 
0
source
 int[] a=new int[5]; 
  • In the above array declaration, int [] is an array of type ie an array of integer types
  • a is the name of the array, for example, to declare the variable 'a' ------> int a; the same path for int [] a arrays, where [] indicates an array declaration only diff is []

now come to new

new - operator, what is the function of this operator?

this new operator goes into heap memory and allocates an array (due to int []) and allocates 5 values ​​inside the array type int (due to int [5]) calls the reference variable. Here is the reference variable arrayname ie a.

any array created on the heap is specified by the reference variable a

  memory diagram --------------------- | in heap memory | | | ||------------------ | || | | | | | | |------------------- | / --------------------- / / a/ 

5 positions as int[5] in the above example, and the reference variable indicates the location of the memory array

0
source

All Articles