The difference is:
- The
length
of an array is NOT about the actual content - it's the dimension! An array can be of length
10, but only contain 2 elements.
- An Object containing elements (A String is an Object, containing multiple chars) might have a certain count of Objects inside. This count can vary, depending on whether you add or remove elements. So the
length()
method is about the actual content!
The reason is, that an array is a language construct - Therefore, whenever you want to know something about the array (it's length or else) You have to retrieve a fixed value from the JVM, rather than calling a method that evaluates something.
(The Array
-Class is just a proxy for the actual array-construct.)
Technically spoken an array
(note the small a
) is just a memory allocation for array.length
elements in the memory. (And that's why you can't simple resize an array, but always need to create a NEW array when changing the dimension - it needs to be relocated (in C that was called realloc
) in memory, so there is enough room for array.length * elementSize
bits.
A List on the other hand does not care about the actual position of it's element in the memory, and therefore allows dynamic resizing at any time. (So it can server a method size()
because it has no dimension, and just needs to return information about it's content.)