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
134

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 Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... The length property returns the length of an array.
Discussions

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
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
6
0
May 24, 2019
How can I count element's length in array?

What have you tried so far?

More on reddit.com
🌐 r/javahelp
16
0
October 27, 2022
Please help me understand why array length -2 ?
The comment explains it. If your array is for example 6 numbers long, you want i to be smaller that 4 (so maximum 3), so you can still access i, i +1 and i + 2. i + 2 in this case would be 3 + 2 at maximum, which is the last index in an array with 6 elements. If you didn't subtract 2,you'd get an exception for attempting to read past the end of the array. More on reddit.com
🌐 r/javahelp
10
0
November 1, 2020
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-in-java
Arrays in Java - GeeksforGeeks
Memory for arrays is always allocated on the heap in Java. The elements in the array allocated by new will automatically be initialized to zero (for numeric types), false (for boolean) or null (for reference types). You can use array literals to initialize an array when declaring it. In this case, the new keyword is not required- ... The length of this array determines the length of the created array.
Published   May 8, 2026
🌐
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:
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-array-size-explained-by-example
Java array size, length and loop examples
Unlike the String and ArrayList, Java arrays do not have a size() or length() method, only a length property. To determine the size of a Java array, query its length property.
🌐
HappyCoders.eu
happycoders.eu › java › array-length-in-java
Array Length in Java
June 12, 2025 - The length of an array is defined when it is initialized. The following code, for example, creates a string array of size four: String[] fruits = {"jujube", "apple", "boysenberry", "cherry"};Code language: Java (java)
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › reflect › Array.html
Array (Java Platform SE 8 )
April 21, 2026 - 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.
🌐
Study.com
study.com › business courses › java programming tutorial & training
Array Lengths in Java | Study.com
The number of elements allocated for a Java array can be determined via the length property. In our example, 'ages.length' has the value of 5. Array elements can also be initialized with values at declaration time through the use of '=' followed ...
🌐
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
🌐
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?

🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › How-do-I-find-the-Java-array-length
How do I find the Java array length?
However, the Java array length does not start counting at zero. When you use the length property to find the size of an array that contains five elements, the value returned for the Java array length is five, not four.
🌐
ClojureVerse
clojureverse.org › questions & help › how to?
How to get the Java array length? - How to? - ClojureVerse
May 24, 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
🌐
NeetCode
neetcode.io › solutions › 1929. concatenation of array
LeetCode 1929 Concatenation of Array Solution & Explanation | NeetCode
4 weeks ago - The problem defines the result array ans such that ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n. Instead of looping through the input twice, we can fill both required positions in the result array simultaneously while iterating through the input array just once. This utilizes the index mapping i and i + n directly. Determine the length n of the input 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.
🌐
LabEx
labex.io › tutorials › java-how-to-calculate-java-array-size-runtime-418024
How to calculate Java array size runtime | LabEx
Every array in Java has a built-in length property that returns the total number of elements:
🌐
Sololearn
sololearn.com › en › Discuss › 1177842 › in-java-array-why-we-are-using-length-to-get-numbers-or-to-check-ex-i-arrlength
In java array why we are using .length to get numbers or ...
March 29, 2018 - Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
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
Answer (1 of 2): I cannot see any reason at all why you would want to do this recursively or with an if-statement. 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...
🌐
Baeldung
baeldung.com › home › java › java array › maximum size of java arrays
Maximum Size of Java Arrays | Baeldung
January 8, 2024 - Since the index of the array is int, the approximate index value can be 2^31 – 1. Based on this approximation, we can say that the array can theoretically hold 2,147,483,647 elements.
🌐
GeeksforGeeks
geeksforgeeks.org › java › difference-between-length-of-array-and-size-of-arraylist-in-java
Difference between length of Array and size of ArrayList in Java - GeeksforGeeks
July 11, 2025 - Array has length property which provides the length of the Array or Array object. It is the total space allocated in memory during the initialization of the array. Array is static so when we create an array of size n then n blocks are created ...