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 find out the length of an array in Java
The OP is checking the length of an int array, so the method can't be used for any other type of array. ... Because he's passing an array holding integers to the function (Java is strongly typed). More on reddit.com
🌐 r/ProgrammerHumor
183
3830
November 12, 2017
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
🌐
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   May 8, 2026
🌐
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 ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › reflect › Array.html
Array (Java Platform SE 8 )
April 21, 2026 - 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
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:
🌐
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
🌐
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.
🌐
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.
🌐
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?

🌐
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 - Hmm, you might be thinking of java.util.List, which has a size() method. Arrays use the field length.
🌐
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?
To find the size of a Java array, make sure you invoke the length property, not the method. To determine the size of an array in your code, you need to use the Java array’s length property field.
🌐
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
🌐
W3Schools
w3schools.com › java › java_arrays.asp
Java Arrays
Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting ... How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum
🌐
Coding Rooms
codingrooms.com › blog › 2d-array-length-java
2D Array Length Java
We use arrayname.length to determine the number of rows in a 2D array because the length of a 2D array is equal to the number of rows it has.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › array length in java
Java Array Length with Examples & Syntax
June 4, 2025 - The .length property is the most direct and common way to get the size of an array. It returns the total number of elements stored. It’s simple and efficient since every array in Java internally stores its length as a final field.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
JavaScript arrays are zero-indexed: the first element of an array is at index 0, the second is at index 1, and so on — and the last element is at the value of the array's length property minus 1.
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-length-vs-length-Whats-the-difference
Java length vs length(): What's the difference?
The key difference between Java’s length variable and Java’s length() method is that the Java length variable describes the size of an array, while Java’s length() method tells you how many characters a text String contains.