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

How to get the Java array length?
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 More on clojureverse.org
🌐 clojureverse.org
0
May 21, 2019
How to find out the length of an array in Java
Oh boy, here we go again. More on reddit.com
🌐 r/ProgrammerHumor
183
3830
November 12, 2017
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
Why is getting the length of an array O(1) and not O(n)?
Most implementations store the length of an array in the object itself. Whenever you insert/remove it gets updated. Thus you're not iterating to get the length you're just getting a value. More on reddit.com
🌐 r/learnprogramming
45
11
November 7, 2022
🌐
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
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-array-size-explained-by-example
Java array size, length and loop examples
To find the size of a Java array, query an array’s length property. The Java array size is set permanently when the array is initialized.
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-in-java
Arrays in Java - GeeksforGeeks
To find the size of array java provides a built-in property called length.
Published   2 weeks ago
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › reflect › Array.html
Array (Java Platform SE 8 )
October 20, 2025 - NegativeArraySizeException - if any of the components in the specified dimensions argument is negative. public static int getLength(Object array) throws IllegalArgumentException · Returns the length of the specified array object, as an int.
🌐
JanBask Training
janbasktraining.com › community › java › arraysize-vs-arraylength
Array.size() vs Array.length | JanBask Training Community
August 24, 2025 - Array.length is used to get the fixed number of elements in an array. Since arrays in Java are of fixed size, this property tells you how many elements the array can hold.
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:
🌐
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
🌐
Reddit
reddit.com › r/programmerhumor › how to find out the length of an array in java
r/ProgrammerHumor on Reddit: How to find out the length of an array in Java
November 12, 2017 - Exception handling should not be a go-to option. They are for when something behaves in a way it shouldn't. In Java you can use myArray.length to easily get the size if the array. In, for example, C, you basically need to keep track of how large your array is.
🌐
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?

🌐
Quora
quora.com › How-do-I-get-the-size-of-an-array-in-Java
How to get the size of an array in Java - Quora
Answer (1 of 6): Steps 1. Open the IDE. Open your IDE of choice, or write the code and execute it through the Command Prompt. 2. Copy and paste the following code: class Size { static int[] numbers = {1, 225, 231, 4, 675}; public static void main(String[] args) { int size = numbers.length...
🌐
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.
🌐
Quora
quora.com › How-do-you-find-the-length-of-an-array-using-an-if-statement-in-Java-and-or-do-so-recursively
How to find the length of an array using an if statement in Java and/or do so recursively - Quora
The only way you can know the length of an array is to look at the public length property. However, as you ask, I will provide an example for an array of int. [code]public static lo...
🌐
Medium
theonlylight.medium.com › java-101-the-difference-between-array-0-length-and-array-length-36231e4e846c
Java 101: The Difference Between array[0].length and array.length | by Light | Medium
December 12, 2023 - Therefore, array.length returns the number of 1D arrays in the 2D array, which is also the number of rows in the 2D array.
🌐
Medium
medium.com › javarevisited › understanding-java-arrays-from-basics-to-length-and-limits-9c30f749f2fd
Understanding Java Arrays: From Basics to Length and Limits | by sajith dilshan | Javarevisited | Medium
August 25, 2025 - Array .length vs String .length(): Arrays → .length, Strings → .length(). Updating: Array size is fixed, but you can always replace elements inside. Loops: Arrays pair perfectly with loops for efficient data handling. Once you’re comfortable with arrays, you’ll find it much easier to transition to flexible collections like ArrayList, which can grow and shrink as needed. ... Follow me for more bite-sized Java tips and dev insights.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › length
Array: length - JavaScript | MDN
The length data property of an Array instance represents the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.
🌐
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.