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.

🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-array-size-explained-by-example
Java array size, length and loop examples
The 32-bit Java int can go to a maximum of 2,147,483,647, so that is the theoretical maximum Java array size. However, virtual machines for different operating systems may not allocate every available bit to the elements in the Java array.
🌐
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
When iterating over an array, we can also determine its size manually. Example: This example, demonstrates how to use for each loop to calculate the length of an array. ... // Java program to demonstrate for loop // to calculate length of all type of Arrays import java.util.*; public class 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
🌐
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   December 21, 2016
🌐
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.
🌐
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?
Then, define two counter variables of integer type to store the count of elements of both arrays. Now, use the for-each loop to iterate and increment the counter variable by one with each iteration. Finally, print the result and exit. import java.util.*; public class Example3 { public static void main(String[] args) { // declaration and initialization of the arrays String[] aray1 = { "Tutorix", "Tutorials", "Point", "Simply", "Easy", "Learning" }; int[] aray2 = {58, 66, 74, 55, 62}; // initial counter variable to store the count of elements int countr1 = 0; int countr2 = 0; // printing the length of both arrays for(String elm : aray1) { countr1++; } for(int elm : aray2) { countr2++; } System.out.println("Length of the String array: " + countr1); System.out.println("Length of the integer array: " + countr2); } }
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › arrays.html
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
Like declarations for variables of other types, an array declaration has two components: the array's type and the array's name. An array's type is written as type[], where type is the data type of the contained elements; the brackets are special symbols indicating that this variable holds an array. The size of the array is not part of its type (which is why the brackets are empty).
🌐
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 ...
Find elsewhere
🌐
Educative
educative.io › answers › how-to-resize-an-array-in-java
How to resize an array in Java
The size of an array is the number of elements it can hold. In Java, the array size is fixed when it is created.
🌐
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.
🌐
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)
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 ...
🌐
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 ...
🌐
CodeAhoy
codeahoy.com › java › array-length
Array Length in Java with Examples | CodeAhoy
January 26, 2020 - The length variable is declared as final because the length of an array is defined only when it is created and cannot be changed later. You can safely use the length field everywhere without worrying about changing it inadvertently. length vs size(): size() is a method which is defined in java.util.Collection class.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › array length in java
Java Array Length with Examples & Syntax
October 26, 2025 - The .length property is the most direct and common way to get the size of an array. It returns the total number of elements stored. It’s simple and efficient since every array in Java internally stores its length as a final field.
🌐
Great Learning
mygreatlearning.com › blog › it/software development › how to find array length in java
How to find Array Length in Java
September 12, 2024 - A. In the ArrayList attribute, there is no length property, but it still gives the length by using the size() method, whereas the length property of the array gives the total number of elements in the array or the length of the array.
🌐
Oreate AI
oreateai.com › blog › understanding-array-size-in-java-a-comprehensive-guide › edc0c22dbe84fdf66968c3d7856cc6ff
Understanding Array Size in Java: A Comprehensive Guide - Oreate AI Blog
January 8, 2026 - You might wonder how to find out how many elements your array can hold after it's been created. In Java, this is done using the length property—an essential aspect that distinguishes arrays from other collections like ArrayLists which utilize ...
🌐
Quora
quora.com › How-do-you-find-an-arrays-length-or-size-in-Java
How to find an array's length or size in Java - Quora
Answer (1 of 25): There are many ways to achieve the same thing: By asking the original Java developer that originally writes the code, “what is your favorite magic number?”. That is called Social Engineering. But it might not work well, as most Java developers have been stereotyped as ...