tags:

views:

208

answers:

7

Is there any reason why an array in Java is an object?

+1  A: 

So that they get all the benefits thereof:

  • getHashCode()
  • toString()

etc.

And arrays aren't 'primitive', so if they can't be primitive, they must be objects.

Noon Silk
In Java, the default `hashCode` and `toString` implementations are used (namely, identity hash code, and `[LFooBar;@deadbeef`) for arrays, so they're not really very useful in general. :-P
Chris Jester-Young
A: 

I'm not sure about the official reason.

However, it makes sense to me that they are objects because operations can be performed on them (such as taking the length) and it made more sense to support these operations as member functions rather than introduce new keywords. Other operations include clone(), the inherited operations of object, etc. Arrays are also hashable and potentially comparable.

This is different from C (and native arrays in C++), where your arrays are essentially pointers to a memory offset.

Uri
Actually, taking the length of a Java array is not an operation. The length is a public member of the object.
Thomas Owens
That's true. I'm not sure why they didn't put in a final getLength() on them.
Uri
Arrays are not comparable using natural order, but of course you're free to write a `Comparator` that works with them.
Chris Jester-Young
@Chris: Of course. But compareTo takes an Object, so whatever you compare (including arrays) has to be an Object.
Uri
+1  A: 

Having arrays be objects means that you can do operations with them (e.g., someArray.count('foo')) instead of just doing it against them (e.g., count(someArray, 'foo')), which leads to more natural syntax.

Ignacio Vazquez-Abrams
Except, in Java, arrays do not have any extra methods, except `clone` (which has the correct covariant return type, is public, and has no checked exceptions). They also have an extra field, `length`. Beyond that, no extra methods or fields beyond what's provided with `Object` are available.
Chris Jester-Young
A: 

Another point is that objects are mutable and are passed by reference. In arrays there aren't any fields/methods that you can use to change "properties" of the array, but you sure can mutate the element values. And the benefits of passing arrays by reference are pretty obvious (though functional programmers probably wish Java had immutable lists passed by value).

Edit: forgot to mention. In the period before autoboxing, it was helpful to be able to store arrays in collections, write them to ObjectStreams etc.

Dan
I wanted to +1 you for the pass by reference, but why would anyone want to pass an immutable thing by value? A big + with immutability is that you can pass by reference without fear of side effects.
ILMTitan
Passing by value doesn't necessarily mean copying the value. Actually in languages with Referential Transparency: http://en.wikipedia.org/wiki/Referential_transparency_%28computer_science%29 the distinction is irrelevant.
Dan
+1  A: 

This link explains why array are objects in Java (on the beggining of the article).

Pedro Ghilardi
+1  A: 

Probably because they wanted to get as close as possible to making everything an object. Native types are there for backward compatibility.

fastcodejava
+5  A: 

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.
Pascal Thivent