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
People also ask

How does the size() method work in Java Lists?
When you call the size() method on a Java List, it does a little bit of internal magic. Rather than iterating through all elements, it directly accesses a variable or field that keeps track of the number of elements in the List, returning that value to you swiftly.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › java list size
Java List Size: Understanding and Utilizing Dynamic Data Structures
Why is the size of a List different from its capacity?
Size and capacity may seem interchangeable, but in the context of a Java List, they convey distinct concepts. The size refers to the actual count of elements in the List, while capacity indicates how many elements it can house before needing to resize. Understanding this distinction helps manage lists more effectively.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › java list size
Java List Size: Understanding and Utilizing Dynamic Data Structures
How often should I call the size() method?
The size() method is there for your use anytime you need to know the number of elements in the List. However, using it excessively or unnecessarily could have performance implications, so employ this tool wisely and only when needed.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › java list size
Java List Size: Understanding and Utilizing Dynamic Data Structures
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › java list size
Java List Size: Understanding and Utilizing Dynamic Data Structures
3 weeks ago - In Java, the List interface provides the size() method to retrieve the number of elements in a List, while the length property is used to determine the array size Java. The size() method is specific to List implementations and provides a dynamic ...
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-array-size-explained-by-example
Java array size, length and loop examples
However, Java arrays do not have a size() method, nor do they have a length() method. Instead, the property length provides a Java array’s size. To further confuse matters, every Java collection class that implements the List interface does have a size() method.
Top answer
1 of 1
8

This question is a duplicate of https://stackoverflow.com/q/300522/773113, but since that question is on stackoverflow, technically it is not a duplicate. (I tried to mark it as a duplicate, but I was prevented, because the other question is not on Programmers SE.)

So, here is what is happening: it is all a matter of convention, and it is all arbitrary. Different languages and environments have their own conventions, (sometimes even self-contradictory,) and you need to learn the conventions of the language you are using, and follow it.

In the old times when C ruled, "size" was the fixed number of bytes allocated for something, while "length" was the smaller, variable number of bytes actually in use. Generally, "size" stood for something fixed, while "length" stood for something variable. But it was still not uncommon for someone to say that "a machine word is 32 bits long" instead of "the size of a machine word is 32 bits", despite the fact that the number of bits in a machine word is, of course, very fixed.

And then comes java, which has arrays of fixed size, but their size is returned via a length property, and strings of fixed size, but their size is returned via a length() method, and collections of variable size, but their length is returned via a size() method. So, java decided to turn things around.

Then came C#, which keeps the term "length" for stuff of fixed size, but for variable size stuff it uses the term "count", which would be perfect, if it was not for the unfortunate fact that besides being a noun it is also a verb, which can be taken to mean that when you get the "count" of a collection, the items in the collection will be counted one by one. (O(N) instead of O(1).)

So, go figure. There is no definitive answer, be sure to carefully study the documentation of the system that you are dealing with, and to understand the precise definition of the terms "length" and "size" within the context of that system, and be even prepared that there may be no precise definition of these terms, and they may be used interchangeably and arbitrarily.

🌐
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
ArrayLists are types of lists in which the internal length can be changed to accommodate more objects as needed when adding them. When it comes to lists, we can find out how many objects are in the list (as opposed to how many it can currently accomodate). Thus, the method size() is used to show how many objects are in the list. ... size() is a method specified in java.util.Collection, which is then inherited by every data structure in the standard library.
🌐
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?
JavaObject Oriented ProgrammingProgramming · 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 space allocated during ...
Find elsewhere
🌐
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 ... called length, which specifies number of buckets in array, while ArrayList is a Collection class, which inherit size() method, which returns number of elements inside Collection.
🌐
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.
🌐
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.
🌐
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 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......
🌐
Baeldung
baeldung.com › home › java › java array › the capacity of an arraylist vs the size of an array in java
The Capacity of an ArrayList vs the Size of an Array in Java | Baeldung
November 11, 2025 - Integer[] array = new Integer[100]; System.out.println("Size of an array:" + array.length); Here, we created an Integer array of size 100, which resulted in the below output · Size of an array:100 · Technically, the default capacity (DEFAULT_CAPACITY) of a newly created ArrayList is 10. However, Java 8 changed how this initial capacity is used for performance reasons. It’s not used immediately and is guaranteed lazily once a new item is added to the list.
🌐
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 ...
🌐
Java67
java67.com › 2016 › 07 › how-to-find-length-size-of-arraylist-in-java.html
How to find length/size of ArrayList in Java? Example | Java67
The size() method returns an integer equal to a number of elements present in the array list. It's different than the length of the array which is backing the ArrayList, which is called the capacity of ArrayList.
🌐
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.
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java arraylist › java arraylist length and size
Java ArrayList Length and Size
August 4, 2023 - An interesting method trimToSize() makes the length of the ArrayList instance to be the list’s current size. An application can use this operation to minimize the storage of an ArrayList instance. The trim operation creates a new backing array with the 'size' attribute and stores the elements in the array. In the new backing array, all array indices store a value. Note that a valid value can be null also. ... The Java ArrayList class does not expose the length of the backing array for simplicity purposes and only exposes the count of currently stored elements in it with the size() method.
🌐
Coderanch
coderanch.com › t › 657113 › certification › size-length-length
size() vs length() vs length [Solved] (Associate Certification (OCAJP 8) forum at Coderanch)
And there is no such thing as an Array class (like String or StringBuilder or ArrayList). So yes, it's unique in the Java core language. Steffe Wilson wrote:Am I ok to think in terms of special case for Arrays (length attribute) and Strings (length() method) whereas all the Collections use size() ?
🌐
W3Schools
w3schools.com › java › ref_arraylist_size.asp
Java ArrayList size() Method
compare() equals() sort() fill() length Java ArrayList Methods · add() addAll() clear() clone() contains ensureCapacity() forEach() get() indexOf() isEmpty() iterator() lastIndexOf() listIterator() remove() removeAll() removeIf() replaceAll() retainAll() set() size() sort() spliterator() subList() toArray() trimToSize() Java LinkedList Methods ·
🌐
Scaler
scaler.com › home › topics › java list size()
Java List Size() - Scaler Topics
April 28, 2024 - You can check whether the list is null or not to avoid it. Assume you have an ArrayList with some names, and you want to use the size() method to find out how big it is. In the code below, we've created an ArrayList of Strings in Java, added two string objects to it with the add() method, and used the size() method to calculate the number of elements in the list.