In Java, why are arrays objects? Are there any specific reasons?
arrays, java
Solution
Because the Java Language Specification says so :)
In the Java programming language arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). All methods of class Object may be invoked on an array.
So, unlike C++, Java provides true arrays as first-class objects:
- There is a `length` member.
- There is a `clone()` method which overrides the method of the same name in class `Object`.
- Plus all the members of the class `Object`.
- An exception is thrown if you attempt to access an array out of bounds.
- Arrays are instanciated in dynamic memory.
Problem
Is there any reason why an array in Java is an object?