length and length() in Java

Let me first highlight three different ways for similar purpose.

lengtharrays (int[], double[], String[]) — to know the length of the arrays

length()String related Object (String, StringBuilder, etc) — to know the length of the String

size()Collection Object (ArrayList, Set, etc) — to know the size of the Collection

Now forget about length() consider just length and size().

length is not a method, so it completely makes sense that it will not work on objects. It only works on arrays.
size() its name describes it better and as it is a method, it will be used in the case of those objects who work with collection (collection frameworks) as I said up there.

Now come to length():
String is not a primitive array (so we can’t use .length) and also not a Collection (so we cant use .size()) that’s why we also need a different one which is length() (keep the differences and serve the purpose).

As answer to Why?
I find it useful, easy to remember and use and friendly.

Leave a Comment