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
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
How to get the Java array length?
Why doesn’t [Ljava.lang.String have a length attribute · So it will not transverse the Array and alenght function will give me the “Array lenght” value · Arrays don’t really have a length field, its Java syntax sugar. Since arrays are primitive types and not real objects More on clojureverse.org
🌐 clojureverse.org
0
May 21, 2019
Difference between length and size?
Length will give you the numbers of items in an array. For example: ... Size... in the following code if you click the button, you will also get a return of 4, because JavaScript makes the browser only select 4 from the list of 7. More on teamtreehouse.com
🌐 teamtreehouse.com
2
June 16, 2017
[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
🌐
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 ...
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-array-size-explained-by-example
Java array size, length and loop examples
Any class that extends the List interface expands dynamically. Java arrays do not expand and contract. 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.
🌐
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 ...
🌐
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.
🌐
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 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......
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › length-vs-length-java
length vs length() in Java - GeeksforGeeks
January 4, 2025 - Java Collection · Last Updated : 4 Jan, 2025 · array.length: length is a final variable applicable for arrays. With the help of the length variable, we can obtain the size of the array. string.length() : length() method is a final method which ...
🌐
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.
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_arrays_length.asp
Java Array length Property
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... The length property returns the length of an array.
🌐
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 key difference between Java’s length variable and Java’s length() method is that the Java length variable describes the size of an array, while Java’s length() method tells you how many characters a text String contains.
🌐
W3Schools
w3schools.com › java › java_arrays.asp
Java Arrays
Use new with a size when you want to create an empty array and fill it later. ... 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
🌐
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
The length property is the most common way to find the size of an array in Java.
Published   July 12, 2025
🌐
Quora
quora.com › What-is-the-difference-between-array-size-and-array-length
What is the difference between array size and array length? - Quora
Answer (1 of 4): * “Length” is generally taken to mean the number of elements in the array. * “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: ...
🌐
Codecademy
codecademy.com › docs › java › arrays › .length
Java | Arrays | .length | Codecademy
April 21, 2025 - Once an array is created, its length is constant and cannot be changed. To work with dynamic sizes, consider using ArrayList. You’ll get an ArrayIndexOutOfBoundsException error, which is a runtime error. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more! ... Learn to code in Java ...
🌐
ClojureVerse
clojureverse.org › questions & help › how to?
How to get the Java array length? - How to? - ClojureVerse
May 21, 2019 - Why doesn’t [Ljava.lang.String have a length attribute · So it will not transverse the Array and alenght function will give me the “Array lenght” value · Arrays don’t really have a length field, its Java syntax sugar. Since arrays are primitive types and not real objects
🌐
HappyCoders.eu
happycoders.eu › java › array-length-in-java
Array Length in Java
June 12, 2025 - This is because, with 32 bits, we can address not just 232 bytes, i.e. 4 GB, but eight times as many, i.e. 32 GB. Elements in primitive arrays each occupy the following memory space: ... The align function rounds the result up to the next value ...