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

terminology - What is the difference between size and length? - Software Engineering Stack Exchange
It seems that the terms size and ... i.e. a length field in a data header is said to indicate the size of the data. Did I see this correctly? If not, how do the terms differ? Which term is most common for which usage? ... I suppose that size is definitely more appropiate if the data is not continuous. Also note that sometimes count is used to denote exactly the same. In many cases the usage is subjective. ... I don't think there's any consistent use. Certainly in Java arrays have ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
November 30, 2015
length and length() in Java
What is the difference between length and length() in Java, and when should you use each? Understand how Java treats arrays and strings differently when it comes to measuring their size or number of elements. More on janbasktraining.com
🌐 janbasktraining.com
1
May 15, 2025
java - Is there a technical difference between the terms "length" and "size" (in programming, of course)? - Stack Overflow
I don't think so. It is more likely caused by the API being work on a large team and different people have different take on what the name should be, especially with words that synonym in certain context, such as size and length in this matter. More on stackoverflow.com
🌐 stackoverflow.com
java - What's the difference between length and length()? - Stack Overflow
I've noticed that when doing the length of an array you write something like: arrayone.length; However, for such things as array lists or a string, you write a bracket on the end, such as the foll... More on stackoverflow.com
🌐 stackoverflow.com
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.

🌐
YouTube
youtube.com › watch
What is the difference between length, length() and size() (Core Java Interview Question #278) - YouTube
What is the difference between length, length() and size() in Java (Core Java Interview Question #278)For any doubts, live training updates and free Courses,...
Published: March 4, 2024
🌐
Coderanch
coderanch.com › t › 657113 › certification › size-length-length
size() vs length() vs length [Solved] (Associate Certification (OCAJP 8) forum at Coderanch)
October 26, 2015 - Otherwise you'll face similar issues as with String and StringBuilder: if you have an ArrayList with 5 Integers and you could change the size directly to 2 or to 10, what should happen? 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.
🌐
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 ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › length-vs-length-java
length vs length() in Java - GeeksforGeeks
January 4, 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.
🌐
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 an array’s length field and Java’s length() method is that the array length field describes the size of an array, while Java’s length() method tells you how many characters a text String contains.
Published: February 3, 2026
🌐
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?
July 30, 2019 - 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
🌐
javaspring
javaspring.net › blog › java-length-vs-size
Java `length` vs `size()`: A Comprehensive Guide — javaspring.net
In Java, length and size() serve different purposes. length is used for arrays and represents the fixed number of elements an array can hold, while size() is used for collections and returns the current number of elements in a collection.
🌐
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
Answer (1 of 7): The length property of an array specifies the number of elements the array is capable of storing. That is it specifies the number of slots present in the array. For example, let us define an array like this:- int [] arr = new int[10]; Now when we say, arr.length, it gives us th...
🌐
Study.com
study.com › business courses › business 104: information systems and computer applications
Java Array Length vs. Size - Lesson | Study.com
January 15, 2019 - To unlock this lesson you must ... sense, they are closely related. However, they differ in that length applies to arrays, and size applies to ArrayLists....
🌐
JanBask Training
janbasktraining.com › community › java › length and length() in java
length and length() in Java | JanBask Training Community
May 15, 2025 - In Java, both length and length() are used to determine the size of something—but they are used in different contexts, and it’s important to know when to use each.
🌐
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 - After all, it’s mostly something developers’ IDEs would fill out or something they usually just Google — or even just guess by trial-and-error. Actually, let’s test out how much you know right now with a little quiz for fun. For each one, write down or type whether you can use length, length(), or size().
🌐
Java67
java67.com › 2014 › 04 › array-length-vs-arraylist-size-java.html
Array length vs ArrayList Size in Java [Example] | Java67
So next time don't confuse between length and size(), array are special object in Java and has attribute called length, which specifies number of buckets in array, while ArrayList is a Collection class, which inherit size() method, which returns number of elements inside Collection...
🌐
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...
🌐
codelessgenie
codelessgenie.com › blog › difference-between-size-and-length-methods
Difference Between size() and length in Java: Array vs ArrayList Methods Explained — CodeLessGenie.com
Direct Access: Elements are accessed via an index (e.g., array[0] for the first element). length Field: Arrays have a public, final instance field called length that returns the total number of elements the array can hold (its capacity).