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
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-array-size-explained-by-example
Java array size, length and loop examples
You can’t change the size of an array in Java once the array is initialized. A common example of the Java array length property being used in code is a program looping through all of the elements in an array.
🌐
Java67
java67.com › 2014 › 04 › array-length-vs-arraylist-size-java.html
Array length vs ArrayList Size in Java [Example] | Java67
I have mentioned this before on ... well. So, use length attribute to get number of elements in a array, also known as length, and for same thing in Collection classes like ArrayList, Vector, use size() method....
🌐
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 - Array has length property which provides the length of the Array or Array object. It is the total space allocated in memory during the initialization of the array. Array is static so when we create an array of size n then n blocks are created of array type and JVM initializes every block by default value. Let's see this in the following figure. On the other hand, java ArrayList does not have length property.
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.

🌐
JanBask Training
janbasktraining.com › community › java › arraysize-vs-arraylength
Array.size() vs Array.length | JanBask Training Community
August 24, 2025 - Array.size() (or simply .size) returns the total number of elements currently present in the array. It’s basically the same as .length in Ruby, as both are interchangeable. ... In Java, you’ll always use .length for arrays and .size() for collections like ArrayList.
🌐
Medium
medium.com › javarevisited › understanding-java-arrays-from-basics-to-length-and-limits-9c30f749f2fd
Understanding Java Arrays: From Basics to Length and Limits | by sajith dilshan | Javarevisited | Medium
August 25, 2025 - Array .length vs String .length(): Arrays → .length, Strings → .length(). Updating: Array size is fixed, but you can always replace elements inside. Loops: Arrays pair perfectly with loops for efficient data handling. Once you’re comfortable with arrays, you’ll find it much easier to transition to flexible collections like ArrayList, which can grow and shrink as needed. ... Follow me for more bite-sized Java tips and dev insights.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › length-vs-length-java
length vs length() in Java - GeeksforGeeks
January 4, 2025 - Example: JAVA · // Java program ... { // Here array is the array name of int type int[] array = new int[4]; System.out.println("The size of the array is " + array.length); // Here str is a string object String str = ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-find-length-or-size-of-an-array-in-java
How to Find Length or Size of an Array in Java? - GeeksforGeeks
... // Java program to demonstrate ... Geeks { public static void main(String[] arg) { int[] arr = { 1, 2, 3, 4, 5, 6, 7 }; int c = 0; for (int i : arr) c++; System.out.println("The Size of the array is " + c); } }...
Published   July 12, 2025
🌐
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 - Remember that this field will not ... stored (irrespective of whether elements are present or not). In the code below, we first initialize an array of length 7....
🌐
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 collections of elements in Java, but they expose their element count through different methods (or operators) because they are different types. ... Array.length is a final public instance field of the array object (an int). ArrayList.size() is a public instance method that returns an int.
🌐
TutorialsPoint
tutorialspoint.com › What-is-the-difference-between-the-size-of-ArrayList-and-length-of-Array-in-Java
What is the difference between the size of ArrayList and length of Array in Java?
ArrayList doesn't have length() method, the size() method of ArrayList provides the number of objects available in the collection. Array has length property which provides the length or capacity of the Array. It is the total spac
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-length-vs-length-Whats-the-difference
Java length vs length(): What's the difference?
The Java String’s length() method complies with this best practice. So why doesn’t an array? The difference harks back to Java’s C -based heritage. The Java programming language was written in such a way that developers who program in C and C++ can more easily adopt this language. Several programming constructs in Java map directly back to how C and C++ programs work. One of these constructs is Java’s main method. Another is the fact that you access the size of an array through the length property, not the length method.
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › difference-between-length-of-array-and-size-of-arraylist-in-java
Difference between length of Array and size of ArrayList in Java
The array has a length method that provides the number of elements can be stored or in simple words capacity of the Array. Also, the length method also defines the total space which has been allocated during the initialization of the array.
🌐
Quora
quora.com › What-is-the-difference-between-array-size-and-array-length
What is the difference between array size and array length? - Quora
* “Size” is the number of bytes that the array occupies in memory - and C provides a built-in function to return it, called “sizeof()”: EXAMPLE: [code]char array1 [ 100 ] ; int array2 [ 100 ...
🌐
BeginnersBook
beginnersbook.com › 2022 › 08 › difference-between-length-of-array-and-size-of-arraylist-in-java
Difference between length of Array and size of ArrayList in Java
The important point to note is: If an array is defined with size 10 like this String arr[] = new String[10]; but we are only adding four elements. The length property for this array would return 10 even though it has only four elements. import java.util.ArrayList; public class JavaExample { ...
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.

🌐
JanbaskTraining
janbasktraining.com › home › difference between array length vs string length () function in java
Difference between Array Length vs String Length () Function in Java
August 8, 2018 - Here, we can say that Array has the length variable that can be used to determine the length of an array, while length function is used with the String class object that can be used to determine the String length. Array variables are mutable, that is once the size of these variables is declared it can be changed during the program or code, while String objects are immutable that means once declared they cannot be modified. In case, if you will modify the String objects, then you will have to create the new one and the older one will not be modified. Read: What Is The Solution Of Java Error: Command Not Found?