ArrayHasLengthMethod

Misconception:

Arrays have a method length() which can be called to determine how many elements they contain.

Incorrect

To get the length of an array, one needs to call its length method

Correct

To get the length of an array, one needs to read its length field

Correction
Here is what's right.

An array has a special immutable field, length, of type int, which holds the length of the array.

There is no method to determine the length, neither length() nor size() exist.

(Arrays have a length field, and ArrayLists have a size() method.)

Value
How can you build on this misconception?

In a clean object-oriented language, every value would be an object. And thus, arrays would be objects. And array types would be classes. As a consequence, to get information about an array (like its length) in a clean object-oriented language, one would invoke methods on that array object.

Thus, the way the Java language presents arrays is not particularly nice, and a student’s expectation that an array has a length method is a wonderful one!

Note that even in Java, you can invoke methods on arrays. Any array type is a subtype of java.lang.Object, and thus it inherits all methods defined in the Object class.

int[] ages = {33, 17, 4, 88};
int c = ages.hashCode();
boolean b = ages.equals(ages);

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.