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
🌐
Oreate AI
oreateai.com › blog › understanding-array-size-in-java-a-comprehensive-guide › 403fa73041aa76255a6e6ab4e83fb3f0
Understanding Array Size in Java: A Comprehensive Guide - Oreate AI Blog
January 8, 2026 - To set up an array, you typically use the new keyword followed by the data type and desired size within square brackets. For instance: ... This line creates an integer array capable of holding five elements. It’s important to note that all elements are initialized to zero if they’re primitive types or null for object types. You might wonder how to find out how many elements your array can hold after it's been created. In Java, this is done using the length property—an essential aspect that distinguishes arrays from other collections like ArrayLists which utilize a method called size().
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
Are 32-Bit indices of arrays a theoretical limitation?
This is a limit that might be removed someday, somehow. Off-heap memory, i.e. the new foreign memory access API, is 64-bit indexed. More on reddit.com
🌐 r/java
23
10
April 27, 2020
How to get array list size in java jsp?
You can use the .size() method of your ArrayList https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html If you do not need the number of the element and only need the element value then you could skip that and just use the enhanced for statement. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html More on reddit.com
🌐 r/javahelp
1
2
November 16, 2014
Does Java ArrayList implement an array or list abstract data type?
An Abstract Data Type (ADT) tells you how a data structure should behave without worrying about how it is implemented. An ArrayList is an implementation of a List ADT. You can also have a LinkedList which is another implementation of a List ADT. The ADT does not care that the ArrayList is implemented with an array and LinkedList is implemented with linked nodes. In addition, there are two types of LinkedList that are the common types. Singly-linked and doubly linked. The List ADT does not care just that the behavior is the same to whomever is using these list implementations. For instance, the List ADT might say the add method adds to the end of the list if no index is specified. So, whether you write an ArrayList or LinkedList when you write the add method with no index specified the item should be added to the end. How you do that is up to you. More on reddit.com
🌐 r/learnprogramming
4
2
December 10, 2021
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.

🌐
CodeAhoy
codeahoy.com › java › array-length
Array Length in Java with Examples | CodeAhoy
January 26, 2020 - The length variable is declared as final because the length of an array is defined only when it is created and cannot be changed later. You can safely use the length field everywhere without worrying about changing it inadvertently. length vs size(): size() is a method which is defined in java.ut...
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-array-size-explained-by-example
Java array size, length and loop examples
The 32-bit Java int can go to a maximum of 2,147,483,647, so that is the theoretical maximum Java array size. However, virtual machines for different operating systems may not allocate every available bit to the elements in the Java array.
🌐
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.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › how-to-determine-length-or-size-of-an-array-in-java
How to determine length or size of an Array in Java?
July 19, 2023 - Length of the String array: 6 Length of the integer array: 5 · In this article, we have learned the use of length property in determining the size of a given array with the help of example programs.
🌐
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.
🌐
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
When iterating over an array, we can also determine its size manually. Example: This example, demonstrates how to use for each loop to calculate the length of an array. ... // Java program to demonstrate for loop // to calculate length of all type of Arrays import java.util.*; public class Geeks { public static void main(String[] arg) { int[] arr = { 1, 2, 3, 4, 5, 6, 7 }; int c = 0; for (int i : arr) c++; System.out.println("The Size of the array is " + c); } }
Published   July 12, 2025
🌐
Educative
educative.io › answers › how-to-resize-an-array-in-java
How to resize an array in Java
The size of an array is the number of elements it can hold. In Java, the array size is fixed when it is created.
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-in-java
Arrays in Java - GeeksforGeeks
Arrays have a fixed size once created. Use ArrayList or Vector for dynamically-sized collections · Memory for arrays is always allocated on the heap in Java.
Published   2 weeks ago
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Arrays.html
Arrays (Java Platform SE 8 )
October 20, 2025 - When the sub-array length reaches ... less than the minimum granularity, then it is sorted using the appropriate Arrays.sort method. The algorithm requires a working space no greater than the size of the original array....
🌐
Quora
quora.com › How-do-you-find-an-arrays-length-or-size-in-Java
How to find an array's length or size in Java - Quora
Answer (1 of 25): There are many ways to achieve the same thing: By asking the original Java developer that originally writes the code, “what is your favorite magic number?”. That is called Social Engineering. But it might not work well, as most Java developers have been stereotyped as not so s...
🌐
HappyCoders.eu
happycoders.eu › java › array-length-in-java
Array Length in Java
June 12, 2025 - This is because, with 32 bits, we can address not just 232 bytes, i.e. 4 GB, but eight times as many, i.e. 32 GB. Elements in primitive arrays each occupy the following memory space: ... The align function rounds the result up to the next value ...
🌐
Codecademy
codecademy.com › docs › java › arrays › .length
Java | Arrays | .length | Codecademy
April 21, 2025 - In Java, the .length property is used to determine the length or size of an array. It is a built-in property for arrays and returns an integer value that represents the number of elements in 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?

🌐
Leyaa
leyaa.ai › codefly › learn › java › part-2 › java-array-length-property
Array length property in Java - Syntax, Examples & Explanation | Leyaa.ai
For dynamic collections, use size() method instead. ... The length property tells you how many elements are in an array.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › array length in java
Java Array Length with Examples & Syntax
October 26, 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.
🌐
Reddit
reddit.com › r/java › are 32-bit indices of arrays a theoretical limitation?
r/java on Reddit: Are 32-Bit indices of arrays a theoretical limitation?
April 27, 2020 -

(If it's wrong here, I'll remove this post)

Arrays in Java can be adressed only with 32-bit indices, that means the maximum size of an array is around 2*109 elements. This is maybe inherited from the beginnings of java in 32-bit. Now we have 64-bit. So we could create bigger arrays and access them with a long.

But do you ever have a problem, that your data is so big, it doesn't fit into a array with 2*109 elements or is it a theoretical limit and has no influence on normal enterprise/scientific code?