views:

143

answers:

1

Arrays are implemented as objects in java right? If so, where could I look at the source code for the array class. I am wondering if the length variable in arrays is defined as a constant and if so why it isn't in all capital letters LENGTH to make the code more understandable.

EDIT: http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html#11358 found where it talks about array initialization and creation expressions thanks to @Yishai and @Lauri

+5  A: 

Although arrays are Objects in the sense that they inherit java.lang.Object, the classes are created dynamically as a special feature of the language. They are not defined in source code.

Consider this array:

   MySpecialCustomObject[] array;

There is no source code for that. You created it in code dynamically.

The reason why length is in lower case and a field is really about the fact that the later Java coding standards didn't exist at the time this was developed. If an array was being developed today, it would probably be a method: getLength().

Length is a final field defined at object construction, it isn't a constant, so some coding standards would not want that to be in upper case. However in general in Java today everything is generally either done as a constant in upper case or marked private with a public getter method, even if it is final.

Yishai
what is the general outline for an array class to be generated?
AFK
@sn3twork, Basically array has all of the methods of object, plus a public final length variable and it has a public clone method, implements Cloneable and Serializable (but I assume for legacy reasons not Iterable). Details are in the JLS: http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
Yishai
@Yishai thanks for the link http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html#11358 I found where it talks about defining arrays using arrays initializers and creation expressions
AFK
Good answer. Even an array of primitives are `Objects`.
fastcodejava