Java: difference between initializing an array of integers using brackets and using a new keyword

When initializing an array of integers using parentheses, does the object create on the heap?

public class Foo {
    public static void main (String[] args) {

        int[] values = {1,2,3};  //1

        int[] list = new int[3];  //2
        list[0] = 1;
        list[1] = 2;
        list[2] = 3;
    }
}
+4
source share
1 answer

Yep, an array is an object , so it gets the space allocated on the heap .

Each type of array, including primitives, has a class. Therefore, when you create an array of primitives int, the JVM creates an instance int[].classon the heap.

+2
source

Source: https://habr.com/ru/post/1687042/


All Articles