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
🌐
Codecademy
codecademy.com › docs › java › arraylist › .size()
Java | ArrayList | .size() | Codecademy
January 27, 2023 - Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more. Beginner Friendly.Beginner Friendly17 hours17 hours · Here, a variable size of type int is initialized with the size of a list. The List interface is an ordered collection of elements and is implemented by various classes such as ArrayList, LinkedList, and Vector: ... The following example creates an ArrayList, uses .add() to append elements to it, and uses .size() to print out its size:
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-array-size-explained-by-example
Java array size, length and loop examples
The size or length count of an array in Java includes both null and non-null characters. Unlike the String and ArrayList, Java arrays do not have a size() or length() method, only a length property. To determine the size of a Java array, query its length property. Here is an example of how to ...
🌐
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 to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
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.
🌐
Baeldung
baeldung.com › home › java › core java › how to get the size of an object in java
How to Get the Size of an Object in Java | Baeldung
January 8, 2024 - For example, when we compare int primitive (which consumes only 4 bytes) to the Integer object which takes 16 bytes, we see that there is 300% memory overhead. One way to get an estimate of an object’s size in Java is to use getObjectSize(Object) ...
🌐
TutorialsPoint
tutorialspoint.com › What-does-the-method-size-do-in-java
What does the method size() do in java?
the size of the list. import ... for (Integer number : arrlist) { System.out.println("Number = " + number); } int retval = arrlist.size(); System.out.println("Size of list = " + retval); } }...
🌐
TutorialsPoint
tutorialspoint.com › how-to-use-list-size-method-in-java-with-examples
How to use List size() method in Java With Examples?
We use this method to retrieve ... list.add(30); System.out.println("The list elements are: " + list); //using size() method System.out.println("The size of list is: " + list.size()); } }...
Find elsewhere
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.

🌐
GeeksforGeeks
geeksforgeeks.org › videos › java-program-to-find-size-of-int-float-double-and-char-in-your-system
Java Program to Find Size of int, float, double and char in your system - GeeksforGeeks | Videos
But in the case of Java, there is no sizeof() operator, we need to calculate the size using the wrapper class using datatype.SIZE and then divide it by 8 to convert it into bytes format.
Published   July 24, 2022
Views   24K
🌐
Vultr
docs.vultr.com › java › standard-library › java › util › ArrayList › size
Java ArrayList size() - Get List Size | Vultr Docs
November 13, 2024 - In this article, you will learn how to use the size() method with ArrayList in Java. This includes scenarios where knowing the size of a list directly impacts program logic and control flow. We'll also look into practical examples where list size plays a critical role, such as during list ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-size-method-in-java-with-examples
ArrayList size() Method in Java with Examples - GeeksforGeeks
July 11, 2025 - // Java program to demonstrate ... n.add(23); // Getting and printing the // size of the ArrayList int s = n.size(); System.out.println("" + s); } } ... Returns Value: This method returns the number of elements in the ...
🌐
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...
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › validation › constraints › Size.html
Size (Java(TM) EE 7 Specification APIs)
"{javax.validation.constraints.Size.message}" public abstract Class<?>[] groups · Default: {} public abstract Class<? extends Payload>[] payload · Default: {} public abstract int min · Returns: size the element must be higher or equal to · Default: 0 · public abstract int max ·
🌐
GeeksforGeeks
geeksforgeeks.org › java › list-size-method-in-java-with-examples
List size() method in Java with Examples - GeeksforGeeks
July 11, 2025 - ... // Java program to Illustrate ... l.add(5); // Getting total size of list // using size() method int s = l.size(); System.out.println("Size : " + s); } }...
🌐
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 - See the example below. public class SimpleTesting { public static void main(String[] args) { int[] intArr = new int[7]; System.out.print("Length of the Array is: " + intArr.size()); } } ... Exception in thread "main" java.lang.Error: Unresolved ...
🌐
TutorialsPoint
tutorialspoint.com › home › java/util › java arraylist size
Java ArrayList Size
September 1, 2008 - This method returns the number of elements in this list. ... The following example shows the usage of Java ArrayList size() method. We're adding couple of Integers to the ArrayList object using add() method calls per element.
🌐
javaspring
javaspring.net › blog › java-length-vs-size
Java `length` vs `size()`: A Comprehensive Guide — javaspring.net
Once an array is created, its length is fixed and cannot be changed. For example, if you create an array of integers with a length of 5, it will always have space for exactly 5 elements.