Re: Extending Arrays
Daniel Pitts wrote:
A Java array is actually *not* a real class. It is a special type of
primitive. From the JVM point of view, it does not have a field called
length, there is actually a special op-code to access the array length...
The JVM opcode does not define or restrict the Java semantics.
From a Java standpoint, a Java array is an instance of a class. A real class.
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.
A variable of array type holds a reference to an object.
The members of an array type are all of the following:
* The public final field length,
From the Java language point of view, the array most definitely does have a
field called 'length'. It says so right in the JLS.
Also, an array reference is assignable to Object, java.io.Serializable,
and Clonable reference variables, but in many regards is very different
than other "classes".
Every array has an associated Class object, shared with all other arrays
with the same component type.
The other thing to consider is that arrays are low-level
building-blocks, and you should avoid using them in all but the
lowest-level abstractions (such as Collection API implementations).
Anything else should use abstractions (such as the Collections API) in
place of these primitives.
OK, that's *a* rule of thumb. I would venture to say that there are many more
use cases for arrays. Also, it isn't accurate to refer to arrays as
"primitives"; arrays are reference (i.e., Object) types, not primitives.