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
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.

🌐
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...
🌐
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 - Unlike the length property of arrays, the value returned by the size() method is not constant and changes according to the number of elements. All the collections of the Collection Framework in Java are dynamically allocated, so the number of elements can vary.
🌐
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.
🌐
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 › length-vs-length-java
length vs length() in Java - GeeksforGeeks
January 4, 2025 - Example: JAVA · // Java program to illustrate the // concept of length // and length() public class Test { public static void main(String[] args) { // 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"; System.out.println("The size of the String is " + str.length()); } } Output ·
Find elsewhere
🌐
Medium
medium.com › swlh › memory-tricks-on-how-to-remember-when-to-use-length-vs-length-vs-size-in-java-22546513f407
Memory Tricks on How to Remember When to Use Length, Length(), and Size() in Java | by Tremaine Eto | The Startup | Medium
December 12, 2021 - Memory Tricks on How to Remember When to Use Length, Length(), and Size() in Java If you ever want to really bring the fun to a Zoom party, then you can quiz your friends on when you use length vs …
🌐
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.
🌐
Coderanch
coderanch.com › t › 657113 › certification › size-length-length
size() vs length() vs length [Solved] (Associate Certification (OCAJP 8) forum at Coderanch)
Therefore you'll have a (read-only) method to get the number of elements in a collection: the size() method. Last but not least, let's have a look at the array. Arrays are special objects in Java, so each array IS-A Object! They have a simple attribute named length which is public and final.
🌐
TutorialsPoint
tutorialspoint.com › what-are-the-differences-between-length-and-length-in-java
What are the differences between length and length () in Java?\\n
In Java, both the length property and the length() method are used to determine the size of data, or we can say they help us to find the number of elements an object contains. However, they are used with different Java objects. The length is an insta
🌐
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.
🌐
JavaBeat
javabeat.net › home › difference between length and length() method in java
Difference Between length and length() Method in Java
October 31, 2023 - For instance, if an array is declared with a total of five elements, implementing the length variable on that particular array will return the size of the array as “5”. The total number of characters available in the String is calculated ...
🌐
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.
🌐
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.
🌐
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
To find the size of ArrayList, we use size() method. // Creating an ArrayList of String type ArrayList fruits = new ArrayList(); // adding elements to ArrayList fruits.add("Apple"); fruits.add("Banana"); fruits.add("Mango"); fruits.add("Grapes"); This is how, you can print the size of ArrayList.
🌐
Interview Kickstart
interviewkickstart.com › home › blogs › learn › length vs. length method in java
Length vs. Length Method in Java | Interview Kickstart
December 18, 2025 - You can use this length attribute or variable to find the size of an array in Java by using the dot operator with the array name. For example, Array.length will give you the length of the array named “Array.”
Address   4701 Patrick Henry Dr Bldg 25, 95054, Santa Clara
(4.7)
🌐
Quora
algomart2.quora.com › What-is-the-difference-between-length-and-size-in-Java
What is the difference between length and size in Java? - AlgoMart2 - Quora
Answer: [code ]size()[/code] is a method specified in [code ]java.util.Collection[/code], [code ]size()[/code] which is then inherited by every data structure in the standard library. length is a field on an array (arrays are objects, you just don't see the class normally), and [code ]length()[/c...
🌐
Blogger
androidcodingworld.blogspot.com › 2017 › 09 › length-vs-length-in-java.html
AndroidCodingWorld: length vs length() in Java
Sample Code: public class Test ... "GeeksforGeeks"; System.out.println("The size of the String is " + str.length()); } } Output: The size of the array is 4 The size of the String is 13 · Posted by · Abhinaw Tripathi at · Wednesday, September 13, 2017 · Email ThisBlogThis!Share ...