Since most other classes seem to let the developer retrieve the length or size of its content by calling a method, usually length()
or size()
, how come the length of an array is retrieved by reading the instance variable length
? It just seems inconsistent to me, especially since a String object is immutable as well and still uses a length()
method.

- 16,038
- 10
- 74
- 104

- 9,270
- 11
- 44
- 64
-
I upvoted this question because it's a great question. However, I also think it's not a "practical, answerable question" (as per the [FAQ](http://stackoverflow.com/faq)), so I may vote to close it. I'm feeling so hypocritical. :( – Ted Hopp May 28 '13 at 18:06
-
It's the way it is because it's the way they designed it. Unless you can get Jim Gosling or Ken Arnold to answer here all you will get is guesswork. Not constructive. – user207421 May 28 '13 at 23:52
5 Answers
It is inconsistent. I suppose the actual reason for the distinction can only be found in the early history of Java language development. Perhaps it was for what seemed at the time to be performance reasons. I suspect that if Java were being (re)designed from scratch today, the length
field would disappear and instead there would be a java.lang.Array
class* (similar to java.lang.Enum
) from which all arrays would derive and which would include a length()
method inherited by all arrays.
Actually, the lack of an Array
class (in the above sense) may indicate why there's a length
attribute: arrays are more "built in" to the language than, say, the collection classes (which are part of a class library).
* Java does have java.lang.reflect.Array
, but that does something completely different from what I'm talking about.

- 232,168
- 48
- 399
- 521
Most of the collections have dynamic sizes, so they need a method to verify the length/size in that specific time. An array has a fixed length, so it doesn't need to be recalculated all the time.

- 2,720
- 2
- 28
- 40
-
Actually, the collection classes don't recalculate all the time. The `size()` methods simply return the value stored in a private field that is maintained as the collection is modified. An identical approach could have been used for arrays. Besides, `String` objects don't have dynamic size (and can even be thought of as arrays of characters) and yet they have a `length()` method rather than a (final) `length` field. – Ted Hopp May 29 '13 at 01:06
An array is of fixed size - it will never change. As such, you don't need the overhead of a method.

- 60,705
- 7
- 138
- 176
-
4Then why does String, which is immutable as well, have a `length()` method? – Jimmy C May 28 '13 at 17:49
-
1@JimmyC `String` implements `CharSequence` interface, that has the `length()` method. – Daniel Pereira May 28 '13 at 17:51
-
1I agree, this does not explain why it is different than everything else in Java. – John B May 28 '13 at 17:51
-
1@DanielPereira - `CharSequence` is a latter addition to Java. From the start, `String` could have had a `length` property, but it didn't. – Ted Hopp May 28 '13 at 17:51
-
-
@JimmyC: It is conceivable that some future variation of `String` might not want to have an `int` field which always holds the length. A future string type might e.g. use the upper bit of its internally-stored "length" field to indicate that the array element beyond the end of the string should be used to store special information about the string's contents [e.g. whether it is known not to contain any characters that cannot be converted to UTF-8 by simply dropping the MSB, or cannot be converted to UTF-32 by simply inserting zeroes]. – supercat Oct 04 '13 at 20:20
The reason why is that the JLS says so:
The members of an array type are all of the following:
- The public final field
length
, which contains the number of components of the array. length may be positive or zero.
Regarding the underlying motivation: only the people who created the language could answer...
Interestingly, Oak, which is the language at the origin of Java, already had that notion. If you read its specifications, you will see that:
The length of any array can be found by using
.length
So the best guess here is that it is the result of a decision made by a few guys more than 20 years ago.
A method is executed every time it's called. Optimization exists, such as cache etc.. but the point is that an array never gets its size changed.
Thus, what could better fit the case than a "final" (not sure about the implementation under the hood but same concept) instance's field?
java.util.Collection
describes some classes wrapping arrays like ArrayList
, HashSet
etc..
In their case, size is altered since the concept of collection is to have an extensible array.
Thus, the size must be calculated thanks to a method (size()
), not a field in this case.

- 21,881
- 15
- 82
- 180