Master length() and size() in Java: A Concise Comparison

Robert Jul 13, 2026

The length() and size() methods in Java are often used interchangeably, but they come from different classes and have slight differences in functionality. Here's a comprehensive look at both, their differences, and when to use each.

Is learning Java worth in 2019?
Is learning Java worth in 2019?

Before diving into these methods, let's clarify the terms. Both length() and size() return the number of elements in an array or a collection of objects in Java. However, the key difference lies in the classes that contain these methods and how they are used.

Java
Java

length vs size in Primitive Arrays

In Java, arrays have a fixed size, and the number of elements in an array is determined at the time of its creation. For primitive arrays, the length property is used to get the number of elements. This property is a variable of the array itself, meaning you can't change its value.

Java programming code abstract technology background | Premium Photo
Java programming code abstract technology background | Premium Photo

Here's an example:

int[] numbers = new int[10];System.out.println(numbers.length);
Creates an array of 10 integersPrints 10
a black and white image of a coffee cup with steam coming out of the top
a black and white image of a coffee cup with steam coming out of the top

The final keyword and length

The length property of an array is final. This means its value cannot be changed once the array is created.

Here's an example of trying to change the length, which will result in a compile-time error:

What is the Future of Java?
What is the Future of Java?
numbers.length = 20;
Compile-time error: Cannot assign a value to final variable 'length'

Using length in for loops

Another common use of the length property is in for loops to iterate through the elements of an array. Here's an example:

☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
for (int i = 0; i < numbers.length; i++) {
   System.out.println(numbers[i]);
Prints all elements in the 'numbers' array.

size() in Collections

Top JDK 20 New Features: Every Java Developer Must Know In 2023
Top JDK 20 New Features: Every Java Developer Must Know In 2023
Java needed in modern world
Java needed in modern world
people sitting at tables in front of a menu on a large display wall behind them
people sitting at tables in front of a menu on a large display wall behind them
java
java
there is a large waterfall coming out of the cave
there is a large waterfall coming out of the cave
Hybrid Women's Denim Jeans Low Rise Stretch Bootcut Black Size 7 64
Hybrid Women's Denim Jeans Low Rise Stretch Bootcut Black Size 7 64
Interface in Java
Interface in Java
a blue background with the word's logo and a white coffee cup on top
a blue background with the word's logo and a white coffee cup on top
Кросплатформність Java: переваги та можливості для розробки універсальних додатків
Кросплатформність Java: переваги та можливості для розробки універсальних додатків
30 Days of Java – Day 7 | Java Arrays Explained for Beginners
30 Days of Java – Day 7 | Java Arrays Explained for Beginners
y2k fashion
y2k fashion
Coffee Shop
Coffee Shop
Online java training – Be a Java Developer!
Online java training – Be a Java Developer!
How to choose Upper & Lower bounds in Java Generics ?
How to choose Upper & Lower bounds in Java Generics ?

The size() method, on the other hand, is used with Collection interfaces and classes that implement them, such as List, Set, and Map. Unlike Java arrays, collections have dynamic sizes, meaning they can grow and shrink as elements are added or removed.

Here's an example of using size() with an ArrayList:

ArrayList<Integer> numberList = new ArrayList<Integer>();
numberList.add(10);
numberList.add(20);
System.out.println(numberList.size());
Prints 2

Using size() in loops

Like length, size() can also be used in for loops:

for (int i = 0; i < numberList.size(); i++) {
   System.out.println(numberList.get(i));
Prints all elements in the 'numberList' ArrayList.

The difference in behavior

Unlike length, size() is not final and its value can be changed. This is because collections like ArrayList, LinkedList, and HashMap allow you to add or remove elements. Furthermore, Collection's size() method takes into account all elements in the collection, including null elements.

In conclusion, while both length() and size() serve the same purpose of determining the number of elements, they differ in the types of data structures they are used with and the capabilities they offer. Be sure to choose the right one based on the specific requirements and context of your Java code.