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 theObjectclass and have access to its methods, such asclone(),equals(),hashCode(),toString(). - Created with
new: Arrays are created using thenewkeyword, like other objects.// Creating an array of integers int[] numbers = new int[10]; - Have a
lengthfield: Arrays have a public, finallengthfield 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.