I just want to add some remarks to the great answer by Fredrik.
The Java Language Specification in Section 4.3.1 states
An object is a class instance or an array.
So array has indeed a very special role in Java. I do wonder why.
One could argue that current implementation array is/was important for a better performance. But than it is an internal structure, which should not be exposed.
They could of course have masked the property as a method call and handled it in the compiler but I think it would have been even more confusing to have a method on something that isn't a real class.
I agree with Fredrik, that a smart compiler optimazation would have been the better choice. This would also solve the problem, that even if you use a property for arrays, you have not solved the problem for strings and other (immutable) collection types, because, e.g., string
is based on a char
array as you can see on the class definition of String
:
public final class String implements java.io.Serializable, Comparable<String>, CharSequence {
private final char value[]; // ...
And I do not agree with that it would be even more confusing, because array does inherit all methods from java.lang.Object
.
As an engineer I really do not like the answer "Because it has been always this way." and wished there would be a better answer. But in this case it seems to be.
tl;dr
In my opinion, it is a design flaw of Java and should not have implemented this way.