Let me first highlight three different ways for similar purpose.

length -- arrays (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.

Answer from Saif on Stack Overflow
Top answer
1 of 8
131

Let me first highlight three different ways for similar purpose.

length -- arrays (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.

2 of 8
27

A bit simplified you can think of it as arrays being a special case and not ordinary classes (a bit like primitives, but not). String and all the collections are classes, hence the methods to get size, length or similar things.

I guess the reason at the time of the design was performance. If they created it today they had probably come up with something like array-backed collection classes instead.

If anyone is interested, here is a small snippet of code to illustrate the difference between the two in generated code, first the source:

public class LengthTest {
  public static void main(String[] args) {
    int[] array = {12,1,4};
    String string = "Hoo";
    System.out.println(array.length);
    System.out.println(string.length());
  }
}

Cutting a way the not so important part of the byte code, running javap -c on the class results in the following for the two last lines:

20: getstatic   #3; //Field java/lang/System.out:Ljava/io/PrintStream;
23: aload_1
24: arraylength
25: invokevirtual   #4; //Method java/io/PrintStream.println:(I)V
28: getstatic   #3; //Field java/lang/System.out:Ljava/io/PrintStream;
31: aload_2
32: invokevirtual   #5; //Method java/lang/String.length:()I
35: invokevirtual   #4; //Method java/io/PrintStream.println:(I)V

In the first case (20-25) the code just asks the JVM for the size of the array (in JNI this would have been a call to GetArrayLength()) whereas in the String case (28-35) it needs to do a method call to get the length.

In the mid 1990s, without good JITs and stuff, it would have killed performance totally to only have the java.util.Vector (or something similar) and not a language construct which didn't really behave like a class but was fast. They could of course have masked the property as a method call and handled it in the compiler but I think it would have been even more confusing to have a method on something that isn't a real class.

🌐
W3Schools
w3schools.com › java › ref_arrays_length.asp
Java Array length Property
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... The length property returns the length of an array.
Discussions

Variable length (Dynamic) Arrays in Java - Stack Overflow
I was wondering how to initialise an integer array such that it's size and values change through out the execution of my program, any suggestions? More on stackoverflow.com
🌐 stackoverflow.com
java - Difference between size and length methods? - Stack Overflow
Is .size() only for arraylists and .length only for arrays? ... This seems programming language specific, and there's no tag. ... The canonical answer: stackoverflow.com/questions/300522/… This one is also good: stackoverflow.com/questions/1965500/length-and-length-in-java More on stackoverflow.com
🌐 stackoverflow.com
Getting the array length of a 2D array in Java - Stack Overflow
Additionally, if the array was defined as: ... Then the code would fail when trying to get the length. Is there a different (more intelligent) way to do this? ... I found the following reference quite comprehensive, programiz.com/java-programming/multidimensional-array More on stackoverflow.com
🌐 stackoverflow.com
Was there a point of time when array.length is a O(N) operation in Java
If you don't store the length and it is a linkedlist for example, you indeed will have to go through all the elements until the list ends, so O(N) For normal arrays tho, I think it was always O(1) More on reddit.com
🌐 r/AskProgramming
13
18
October 22, 2021
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-find-length-or-size-of-an-array-in-java
How to Find Length or Size of an Array in Java? - GeeksforGeeks
The length property is the most common way to find the size of an array in Java.
Published   July 12, 2025
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › reflect › Array.html
Array (Java Platform SE 8 )
October 20, 2025 - IllegalArgumentException - if componentType is Void.TYPE or if the number of dimensions of the requested array instance exceed 255. NegativeArraySizeException - if the specified length is negative
🌐
GeeksforGeeks
geeksforgeeks.org › java › length-vs-length-java
length vs length() in Java - GeeksforGeeks
January 4, 2025 - 3. To directly access a field member of an array we can use .length; whereas .length() invokes a method to access a field member. Example: JAVA ·
Find elsewhere
🌐
Codecademy
codecademy.com › docs › java › arrays › .length
Java | Arrays | .length | Codecademy
April 21, 2025 - The .length property returns an integer value that represents the number of elements in the array. This example uses the .length property to determine the array length:
🌐
TutorialsPoint
tutorialspoint.com › what-is-length-in-java-arrays
What is length in Java Arrays?
February 19, 2020 - Length is a filed in java, it gives the total number of the elements in a Java array. The length of an array is defined after the array was created. Integer[] myArray = {23, 93, 56, 92, 39}; System.out.p
🌐
HappyCoders.eu
happycoders.eu › java › array-length-in-java
Array Length in Java
June 12, 2025 - The height is the length of the array (i.e., the number of rows the matrix contains), and the width is the length of the first row. Please note, however, that with a two-dimensional array in Java, all sub-arrays do not necessarily have to be ...
🌐
Reddit
reddit.com › r/askprogramming › was there a point of time when array.length is a o(n) operation in java
r/AskProgramming on Reddit: Was there a point of time when array.length is a O(N) operation in Java
October 22, 2021 -

So I remember my professor back in college tell me that in Java array.length is an O(N) operation, since the implementation would need to go through the array to count everything. That is why I should keep the array length in a variable so that I only call array.length once.

Recently this was brought up in a conversation, and I looked it up. Array.length is a O(1) operation since the length is just a property. My professor was old, so perhaps back in the day, it was a true statement?

Edit: there might be a chance that I misremembered, and it was arraylist. Though arraylist.length should still be O(1), unless it wasn't at some point?

🌐
ClojureVerse
clojureverse.org › questions & help › how to?
How to get the Java array length? - How to? - ClojureVerse
May 21, 2019 - Why doesn’t [Ljava.lang.String have a length attribute · So it will not transverse the Array and alenght function will give me the “Array lenght” value · Arrays don’t really have a length field, its Java syntax sugar. Since arrays are primitive types and not real objects
🌐
YouTube
youtube.com › watch
How to find length of array in Java tutorial - YouTube
Java program to find length of array
Published   January 11, 2023
🌐
Great Learning
mygreatlearning.com › blog › it/software development › how to find array length in java
How to find Array Length in Java
September 12, 2024 - But you can use the length attribute to find the length of an array. When we declare an array, the total number of elements in it is called the array's length, or we can also call it the size of the array.
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-in-java
Arrays in Java - GeeksforGeeks
Traversing an array means accessing each element one by one. In Java, arrays can be easily traversed using a loop where the loop variable runs from 0 to array.length - 1.
Published   2 weeks ago
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › arrays.html
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
Here the length of the array is determined by the number of values provided between braces and separated by commas. You can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of brackets, such as String[][] names.