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
🌐
Baeldung
baeldung.com › home › java › java io › file size in java
File Size in Java | Baeldung
November 29, 2023 - The above code first checks if the file exists and if it does, it calculates and prints its size in bytes, kilobytes, megabytes, and gigabytes. In this tutorial, we illustrated examples of using Java and Apache Commons IO to calculate the size of a file in the file system.
🌐
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!
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
Is .size() only for arraylists and .length only for arrays? ... This seems programming language specific, and there's no tag. ... The canonical answer: stackoverflow.com/questions/300522/… This one is also good: stackoverflow.com/questions/1965500/length-and-length-in-java 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
memory - Calculate size of Object in Java - Stack Overflow
I want to record how much memory (in bytes, hopefully) an object takes up for a project (I'm comparing sizes of data structures) and it seems like there is no method to do this in Java. Supposedly,... More on stackoverflow.com
🌐 stackoverflow.com
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 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
🌐
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).
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-array-size-explained-by-example
Java array size, length and loop examples
Note that array length in Java is a property, not a method. That means you can’t follow the word length with round brackets. To set the size of a Java array, explicitly state the intended capacity of the array when you initialize it with the new keyword, as follows:
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.

Find elsewhere
🌐
Scaler
scaler.com › home › topics › java list size()
Java List Size() - Scaler Topics
April 28, 2024 - It is used to calculate the number of objects in an ArrayList. It Does not take any input parameter, and whenever the ArrayList calls this function, it returns an integer representing the count of elements of ArrayList.
🌐
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 ...
🌐
Vultr
docs.vultr.com › java › standard-library › java › util › ArrayList › size
Java ArrayList size() - Get List Size | Vultr Docs
November 13, 2024 - The size() method in Java's ArrayList class is an essential tool for managing collections. It determines the number of elements stored in an ArrayList, which is crucial for operations such as iteration, condition checks, and data manipulation ...
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › java list size
Java List Size: Understanding and Utilizing Dynamic Data Structures
3 weeks ago - The size() method is invoked on an instance of the List interface, and it returns an integer value representing the number of elements in the List. The Java List size() method does not accept any parameters.
🌐
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 ·
🌐
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
Arrays and ArrayLists both hold ... public instance field of the array object (an int). ArrayList.size() is a public instance method that returns an int....
🌐
Coderanch
coderanch.com › t › 411644 › java › declare-size-Array-java
Why cant we declare the size of Array in java? (Beginning Java forum at Coderanch)
August 28, 2008 - It is given in the book, that whenever we declare array in java we cant give size of that array? it sayz like this : This is because you are *not* declaring an array. You are declaring a reference to an array. The size of an array is with the array object, and a reference can refer to any size of array.
🌐
GeeksforGeeks
geeksforgeeks.org › java › list-size-method-in-java-with-examples
List size() method in Java with Examples - GeeksforGeeks
July 11, 2025 - Return Value: This method returns the number of elements in this list. ... // Java program to Illustrate size() method // of List class for Integer value import java.util.*; public class GFG { public static void main(String[] arg) { // Creating object of ArrayList class List<Integer> l = new ArrayList<Integer>(); // Adding Element l.add(1); l.add(2); l.add(3); l.add(4); l.add(5); // Getting total size of list // using size() method int s = l.size(); System.out.println("Size : " + s); } }
🌐
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 - The java ArrayList has size() method for ArrayList which provides the total number of objects available in the collection.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › datatypes.html
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency. boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › List.html
List (Java Platform SE 8 )
October 20, 2025 - Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ?