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
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › list-size-method-in-java-with-examples
List size() method in Java with Examples - GeeksforGeeks
July 11, 2025 - That is, the list size() method returns the count of elements present in this list container. ... // Java Program to demonstrate // List size() Method import java.util.*; class GFG { public static void main (String[] args) { // Declared List ...
🌐
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 ...
🌐
W3Schools
w3schools.com › java › ref_arraylist_size.asp
Java ArrayList size() Method
import java.util.ArrayList; public ... cars.add("Mazda"); System.out.println(cars.size()); } } ... The size() method indicates how many elements are in the list....
🌐
Scaler
scaler.com › home › topics › java list size()
Java List Size() - Scaler Topics
April 28, 2024 - Java List size() method returns the count of elements present in the ArrayList. For example, if an ArrayList has five string objects stored in it, the size method will return five.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › get-the-size-of-an-arraylist-in-java
Get the Size of an ArrayList in Java
Finally, use the size() method to check and retrieve the total number of elements present in the list. A program that demonstrates this is given as follows ? import java.util.ArrayList; import java.util.List; public class Demo { public static void main(String[] args) { List aList = new ArrayList(); ...
🌐
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
You can use the size() method of java.util.ArrayList to find the length or size of ArrayList in Java. The size() method returns an integer equal to a number of elements present in the array list.
🌐
Vultr
docs.vultr.com › java › standard-library › java › util › ArrayList › size
Java ArrayList size() - Get List Size | Vultr Docs
November 13, 2024 - Key to this approach is using the list's size to dictate the loop's boundaries, ensuring that the iteration does not exceed the list's current size and avoids IndexOutOfBoundsException. Understand that size() dynamically updates as elements are added or removed. Clear an ArrayList using clear() and check the size again to confirm it reflects the removals. ... import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); list.add("Cherry"); System.out.println("Initial size: " + list.size()); // Outputs 3 list.clear(); System.out.println("Size after clear: " + list.size()); // Outputs 0 } } Explain Code
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-find-the-length-size-of-an-arraylist
Java Program to Find the Length/Size of an ArrayList - GeeksforGeeks
July 11, 2025 - // Java program to find the size // of an ArrayList import java.util.*; public class GFG { public static void main(String[] args) throws Exception { // Creating object of ArrayList<Integer> ArrayList<Integer> arrlist = new ArrayList<Integer>(); // Populating arrlist arrlist.add(1); arrlist.add(2); arrlist.add(3); arrlist.add(4); arrlist.add(5); // print arrlist System.out.println("ArrayList: " + arrlist); // getting total size of arrlist // using size() method int size = arrlist.size(); // print the size of arrlist System.out.println("Size of ArrayList = " + size); } }
🌐
iO Flood
ioflood.com › blog › length-of-arraylist-java
Java ArrayList Length: How to Get the Size of an ArrayList
February 27, 2024 - The capacity of an ArrayList refers to the amount of memory allocated for storing elements, regardless of whether those positions are currently holding elements or not. If you’re interested in the capacity, you’ll need to use different techniques, which we’ll cover in the advanced section. In Java, you can have an ArrayList of ArrayLists, also known as a multi-dimensional ArrayList. In such cases, finding the length of an individual ArrayList within this structure might be a bit tricky. Let’s look at an example: ArrayList<ArrayList<String>> multiList = new ArrayList<>(); ArrayList<String> list1 = new ArrayList<>(Arrays.asList('Hello', 'World')); ArrayList<String> list2 = new ArrayList<>(Arrays.asList('Java', 'Programming')); multiList.add(list1); multiList.add(list2); for (ArrayList<String> list : multiList) { System.out.println(list.size()); } # Output: # 2 # 2
🌐
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 ... 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.
🌐
Tabnine
tabnine.com › home page › code › java › java.util.list
java.util.List.size java code examples | Tabnine
Headers=" + javaResponseHeaders); } return values.get(0); } ... public static String getVerticalFormattedOutput(List<Map<String, Object>> content, String[] titleFields) { List<String[]> printContents = new ArrayList<String[]>(); int maxCol = content.size() > MAX_COL ?
🌐
Java67
java67.com › 2014 › 04 › array-length-vs-arraylist-size-java.html
Array length vs ArrayList Size in Java [Example] | Java67
Java native arrays have built-in length attribute but no size() method while the Java library containers, known as Collection classes like ArrayList<>, Vector<>, etc, all have a size() method.
🌐
TutorialsPoint
tutorialspoint.com › how-do-i-set-the-size-of-a-list-in-java
How do I set the size of a list in Java?
If we use this method to initialize a list, it won't allow us to add or remove elements from the list, as it creates an immutable list. ... In the below example, we will create a list of integers with a specific size using the Collections.nCopies() method. If you try to add an element to this list, it will throw an UnsupportedOperationException because the list is immutable. import java.util.Collections; import java.util.List; public class SetSizeOfList { public static void main(String[] args) { List<Integer> list = Collections.nCopies(5, 0); System.out.println("List of size 5: " + list); // list.add(1); } }
🌐
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.
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.