size() is a method specified in java.util.Collection, which is then inherited by every data structure in the standard library. length is a field on any array (arrays are objects, you just don't see the class normally), and length() is a method on java.lang.String, which is just a thin wrapper on a char[] anyway.

Perhaps by design, Strings are immutable, and all of the top-level Collection subclasses are mutable. So where you see "length" you know that's constant, and where you see "size" it isn't.

Answer from MattPutnam on Stack Overflow
🌐
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 is static so when we create an array of size n then n blocks are created of array type and JVM initializes every block by default value. Let's see this in the following figure. On the other hand, java ArrayList does not have length property.
Discussions

[Java] size vs. length
Arrays are fixed length so they use a field/variable that gets set on initialization. Lists are variable length so they need a method to figure out their own size. More on reddit.com
🌐 r/learnprogramming
7
3
July 16, 2013
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
🌐
Study.com
study.com › business courses › business 104: information systems and computer applications
Java Array Length vs. Size - Lesson | Study.com
January 15, 2019 - To unlock this lesson you must ... sense, they are closely related. However, they differ in that length applies to arrays, and size applies to ArrayLists....
🌐
Delft Stack
delftstack.com › home › howto › java › size vs length in java
The Difference Between Size and Length in Java | Delft Stack
October 12, 2023 - Unlike the length property of arrays, the value returned by the size() method is not constant and changes according to the number of elements. All the collections of the Collection Framework in Java are dynamically allocated, so the number of ...
🌐
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 an array’s length field and Java’s length() method is that the array length field describes the size of an array, while Java’s length() method tells you how many characters a text String contains.
Published: February 3, 2026
🌐
Study.com
study.com › business courses › business 104: information systems and computer applications
Java Array Length vs. Size - Video | Study.com
January 15, 2019 - There are so many options on Study.com! I can research almost any subject, delve into it more deeply if I wish, and begin studying at a deeper level right away. ... Catherine S. ... Java arrays are ordered collections of elements, all of the same type.
🌐
TutorialsPoint
tutorialspoint.com › What-is-the-difference-between-the-size-of-ArrayList-and-length-of-Array-in-Java
What is the difference between the size of ArrayList and length of Array in Java?
July 30, 2019 - ArrayList doesn't have length() method, the size() method of ArrayList provides the number of objects available in the collection. Array has length property which provides the length or capacity of the Array. It is the total spac
🌐
Quora
quora.com › What-is-the-difference-between-length-of-array-and-size-of-ArrayList-in-Java
What is the difference between length() of array and size() of ArrayList in Java? - Quora
Answer (1 of 7): The length property of an array specifies the number of elements the array is capable of storing. That is it specifies the number of slots present in the array. For example, let us define an array like this:- int [] arr = new int[10]; Now when we say, arr.length, it gives us th...
Find elsewhere
🌐
codelessgenie
codelessgenie.com › blog › difference-between-size-and-length-methods
Difference Between size() and length in Java: Array vs ArrayList Methods Explained — CodeLessGenie.com
ArrayList is a resizable, dynamic array implementation of the List interface in Java’s Collections Framework. Unlike arrays, ArrayList can grow or shrink in size automatically as elements are added or removed.
🌐
JanBask Training
janbasktraining.com › community › java › arraysize-vs-arraylength
Array.size() vs Array.length | JanBask Training Community
August 24, 2025 - Array.size() (or simply .size) returns the total number of elements currently present in the array. It’s basically the same as .length in Ruby, as both are interchangeable. ... In Java, you’ll always use .length for arrays and .size() for ...
🌐
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 get the size of a Java array, you use the length property. To get the size of an ArrayList, you use the size() method. Know the difference between the Java array length and the String’s length() method.
Published: July 4, 2026
🌐
Java67
java67.com › 2014 › 04 › array-length-vs-arraylist-size-java.html
Array length vs ArrayList Size in Java [Example] | Java67
So next time don't confuse between length and size(), array are special object in Java and has attribute called length, which specifies number of buckets in array, while ArrayList is a Collection class, which inherit size() method, which returns ...
🌐
Javatpoint
javatpoint.com › difference-between-length-of-array-and-size-of-arraylist-in-java
Difference between Length of Array and Size of ArrayList in Java - javatpoint
Difference between Length of Array and Size of ArrayList in Java with list, set, map, queue, arraylist, linkedlist, hashset, linkedhashset, treeset, hashmap, linkedhashmap, storing and fetching data etc.
🌐
TutorialsPoint
tutorialspoint.com › difference-between-length-of-array-and-size-of-arraylist-in-java
Difference between length of Array and size of ArrayList in Java
September 18, 2019 - Also during initialization what ... every block by a default value. On the other hand, ArrayList does not have length method but it has method named as size for calculating a number of elements get stored in it....
Top answer
1 of 8
136

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
28

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.

🌐
UPI Study
upistudy.com › blog › introduction-to-java › what-is-array-length-in-java
What Is Array Length in Java?
3 weeks ago - The phrase is array length in java comes up a lot in search because students want one clean rule. Here it is: arrays use length, collections like ArrayList use size(). One is a field. The other is a method.
🌐
Quora
quora.com › What-is-the-difference-between-length-and-size-in-Java
What is the difference between length and size in Java? - Quora
Answer (1 of 4): * In java size is method which is written as size() which is available for collections. size() returns number of elements which is contain by collection (not the capacity). * where as .length is a field which works with arrays and gives capacity of arrays. * also length is a m...
🌐
Sitesbay
sitesbay.com › java › java-array
Array in java | Difference Between Length and Length() in Java
4) The maximum allowed size of array in Java is 2147483647 (It is the maximum value of int data type) length: It is a final variable and only applicable for array.
🌐
javaspring
javaspring.net › blog › java-length-vs-size
Java `length` vs `size()`: A Comprehensive Guide — javaspring.net
In Java, length and size() serve different purposes. length is used for arrays and represents the fixed number of elements an array can hold, while size() is used for collections and returns the current number of elements in a collection.