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
🌐
Medium
medium.com › @suim5854 › length-length-size-in-java-fc31279a054d
length, length(), size() in JAVA - juholee - Medium
January 17, 2024 - size()컬레션의 길이를 알고싶을때 사영 · int[] c = new int[7]; c.length ==7 · String a = “asd” · a.length() == 3 · ArrayList<Integer> b = new ArrayList<>(); b.size() == 0 · 0 followers · ·2 following · Help · Status · About · Careers ·
🌐
Quora
quora.com › How-do-we-calculate-the-size-of-the-object-in-Java
How do we calculate the size of the object in Java? - Quora
Answer (1 of 5): An odd one, this, as Java aims to abstract you away from memory. It’s not usually a concern of the programmer. I suppose you could take the object and serialize it into something that counted bytes?
Discussions

arrays - length and length() in Java - Stack Overflow
You can do infinite loops, recursion ... without java trying to stop you from it. It is a good reason for not storing the length in a variable but it sure isn't the reason why it is designed that way. 2009-12-27T20:29:20.227Z+00:00 ... Whenever an array is created, its size is ... More on stackoverflow.com
🌐 stackoverflow.com
java - Difference between size and length methods? - Stack Overflow
What is the difference between .size() and .length ? Is .size() only for arraylists and .length only for arrays? More on stackoverflow.com
🌐 stackoverflow.com
[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 won't me size() method work?
A handy way to initialise your array is to use Arrays.asList("First", "Second", "Third", "Fourth", "Fifth"); More on reddit.com
🌐 r/java
7
0
February 9, 2014
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
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
Why is the time complexity of the size() method constant?
The efficiency of the size() method is a boon. It boasts a constant time complexity because it merely retrieves the stored size value. It doesn't need to traverse through the List's elements, ensuring a quick and efficient response regardless of the List's size.
🌐
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 - The capacity refers to the maximum number of elements the ArrayList can hold without resizing, while the size represents the actual number of elements currently stored in the ArrayList. The time complexity of the Java List size() method is constant, denoted as O(1).
🌐
DEV Community
dev.to › dayanandaeswar › how-to-estimate-java-object-size-1jgp
How to estimate Java object size - DEV Community
November 25, 2024 - In Java, objects are the basic building blocks of any applications. When an object is created, memory is allocated from the heap to store its instance variables. Understanding how much memory an object consumes is important for optimizing memory usage and preventing OutOfMemoryErrors. It is important to optimize the memory consumption especially in cloud solutions. Object size is calculated as Object Header size + variable size(for primitives) or refence size(for objects).
🌐
KristV
kristv.com › news › local-news › blue-bell-launches-honey-vanilla-its-first-new-ice-cream-flavor-of-2026-in-stores-today
Blue Bell launches Honey Vanilla, its first new ice cream flavor of 2026, in stores today
4 days ago - Blue Bell is kicking off 2026 with a new Honey Vanilla Ice Cream, now available in pint size for a limited time. Fan-favorite Java Jolt is also returning to stores this month.
Find elsewhere
🌐
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 ...
🌐
Oracle
docs.oracle.com › en › java › javase › 22 › docs › api › java.desktop › java › awt › Dimension.html
Dimension (Java SE 22 & JDK 22)
July 16, 2024 - Sets the size of this Dimension object to the specified width and height in double precision.
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.

🌐
W3Schools
w3schools.com › java › ref_arraylist_size.asp
Java ArrayList size() Method
The size() method indicates how many elements are in the list. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want ...
🌐
Wikipedia
en.wikipedia.org › wiki › Java
Java - Wikipedia
2 days ago - With a population of 156.9 million people (including Madura) in mid 2024, projected to have risen to 158 million by mid-2025, Java is the world's most populous island, home to approximately 56% of the Indonesian population while constituting ...
🌐
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.
🌐
W3Schools
w3schools.com › java › java_arrays_loop.asp
Java Loop Through an Array
abs() acos() addExact() asin() atan() atan2() cbrt() ceil() copySign() cos() cosh() decrementExact() exp() expm1() floor() floorDiv() floorMod() getExponent() hypot() IEEEremainder() incrementExact() log() log10() log1p() max() min() multiplyExact() negateExact() nextAfter() nextDown() nextUp() pow() random() rint() round() scalb() signum() sin() sinh() sqrt() subtractExact() tan() tanh() toDegrees() toIntExact() toRadians() ulp() Java Output 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
🌐
iO Flood
ioflood.com › blog › length-java
.Length() Java: A Guide for Beginners to Experts
March 4, 2024 - In Java, .length is used to get the length of an array or a string, with the syntax int length = str.length();. It’s a property that helps you determine the size of your arrays and strings, making it a handy tool in your Java programming toolkit.
🌐
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).
🌐
W3Schools
w3schools.com › java › java_data_types.asp
Java Data Types
abs() acos() addExact() asin() atan() atan2() cbrt() ceil() copySign() cos() cosh() decrementExact() exp() expm1() floor() floorDiv() floorMod() getExponent() hypot() IEEEremainder() incrementExact() log() log10() log1p() max() min() multiplyExact() negateExact() nextAfter() nextDown() nextUp() pow() random() rint() round() scalb() signum() sin() sinh() sqrt() subtractExact() tan() tanh() toDegrees() toIntExact() toRadians() ulp() Java Output 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
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › validation › constraints › Size.html
Size (Java(TM) EE 7 Specification APIs)
javax.validation.constraints · @Target(value={METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,PARAMETER}) @Retention(value=RUNTIME) @Documented @Constraint(validatedBy={}) public @interface Size · The annotated element size must be between the specified boundaries (included).
🌐
Reddit
reddit.com › r/java › why won't me size() method work?
r/java on Reddit: Why won't me size() method work?
February 9, 2014 -
import java.util.*;

public class ArrayListTest {
	public static void main(String[] args) {
		ArrayList<String> arrList = new ArrayList<String>();
		String[] items = { "First", "Second", "Third", "Forth", "Fifth" };
		for(String x: items){
			arrList.add(x);
		}
		int size = items.size();
		System.out.println(size);
	}
}

I cant get the size() method to work. What did I do wrong. Sorry if I'm not following this sub's reddiquette

🌐
Codecademy
codecademy.com › docs › java › arraylist › .size()
Java | ArrayList | .size() | Codecademy
January 27, 2023 - The .size() method of the ArrayList class returns the number of elements in the list. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!