Sobes.tech
Junior

Is the array a primitive or an object?

sobes.tech AI

Answer from AI

In Java, arrays are objects.

Here are the main reasons:

  • They inherit from Object: All arrays implicitly inherit from the Object class and have access to its methods, such as clone(), equals(), hashCode(), toString().
  • Created with new: Arrays are created using the new keyword, like other objects.
    // Creating an array of integers
    int[] numbers = new int[10];
    
  • Have a length field: Arrays have a public, final length field that stores the size of the array. Primitive types do not have such fields.
    // Accessing the size of the array
    int size = numbers.length;
    
  • Located on the heap: Arrays, like objects, are located on the heap. Primitives are stored on the stack (except when they are fields of objects).

Although array elements can be primitives, the container itself — the array — is an object.