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

Discussions

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
[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
Exceeding a byte[] array size limit? (Java)

No, that is the maximum size of a Java array.

Why do you want to keep a multi-gig file in memory all at once?

More on reddit.com
🌐 r/learnprogramming
6
9
May 27, 2010
How to find out the length of an array in Java
Oh boy, here we go again. More on reddit.com
🌐 r/ProgrammerHumor
183
3830
November 12, 2017
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › How-do-I-find-the-Java-array-length
How do I find the Java array length?
Use the length property to find the size of a Java array. Do not treat the property as a method. If you put round brackets on a Java array’s length method, the following compile time error will occur: Java Array Length Error: Cannot invoke ...
🌐
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.
🌐
Codecademy
codecademy.com › docs › java › arrays › .length
Java | Arrays | .length | Codecademy
April 21, 2025 - In Java, the .length property is used to determine the length or size of an array. It is a built-in property for arrays and returns an integer value that represents the number of elements in the array.
🌐
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 ...
🌐
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
Find elsewhere
🌐
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 › arrays-in-java
Arrays in Java - GeeksforGeeks
Arrays have a fixed size once created. Use ArrayList or Vector for dynamically-sized collections · Memory for arrays is always allocated on the heap in Java.
Published   2 weeks ago
🌐
Baeldung
baeldung.com › home › java › java array › extending an array’s length
Extending an Array's Length | Baeldung
April 4, 2025 - The way Arrays.copyOf works is that it takes the srcArray and copies the number of elements specified in the length argument to a new array which it internally creates. The size of the new array is the argument that we provide.
🌐
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 ...
🌐
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
🌐
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:
🌐
Pluralsight
pluralsight.com › tech insights & how-to guides › software development
How to initialize an array in Java | Pluralsight
Java will allocate an array of size 3, since there are three items listed (and make no judgment about you adding broccoli cheese to the menu). To make sure nothing is lost in translation, you can explicitly specify the datatype on both sides ...
🌐
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
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Arrays.html
Arrays (Java Platform SE 8 )
October 20, 2025 - The sorting algorithm is a parallel ... array into sub-arrays that are themselves sorted and then merged. When the sub-array length reaches a minimum granularity, the sub-array is sorted using the appropriate Arrays.sort method. If the length of the specified array is less than the minimum granularity, then it is sorted using the appropriate Arrays.sort method. The algorithm requires a working space no greater than the size of the original ...
🌐
Oreate AI
oreateai.com › blog › unpacking-java-arrays-your-friendly-guide-to-storing-collections-of-data › 1fa3042ba694b38d554f8f1dce8a101b
Unpacking Java Arrays: Your Friendly Guide to Storing Collections of Data - Oreate AI Blog
2 weeks ago - This article demystifies Java arrays, explaining them as fixed-size containers for same-type elements. It covers one-dimensional and two-dimensional arrays, how to declare and access elements, and useful utility methods.
🌐
TutorialsPoint
tutorialspoint.com › how-to-determine-length-or-size-of-an-array-in-java
How to determine length or size of an Array in Java?
July 19, 2023 - In Java, one of the convenient ways to determine the length or size of an array is by using its length property. It counts the number of elements stored in an array and returns the count. Finding the length of an array is one of the common yet crucial operation as it is used to find the number ...