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.

🌐
Javatpoint
javatpoint.com › how-to-find-array-length-in-java
How to Find Array Length in Java - Javatpoint
How to Find Array Length in Java - How to Find Array Length in Java with java tutorial, features, history, variables, object, class, programs, operators, for-loop, oops concept, array, string, map, math, methods, examples etc.
🌐
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
🌐
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:
🌐
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 of array.
🌐
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?
Length of the String array: 6 Length of the integer array: 5 · In this article, we have learned the use of length property in determining the size of a given array with the help of example programs.
Find elsewhere
🌐
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
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
🌐
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 ...
🌐
iO Flood
ioflood.com › blog › java-array-length
Java Array Length: How To Get the Array Size in Java
February 27, 2024 - By using the ‘length’ property (array.length), we’re able to quickly determine the size of the array, which in this case, outputs ‘5’. This is the basic way to find the length of an array in Java, but there’s much more to learn about handling arrays in Java.
🌐
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 - But you can use the length attribute to find the length of an array. When we declare an array, the total number of elements in it is called the array's length, or we can also call it the size of the array.
🌐
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 ...
🌐
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.
🌐
Quora
quora.com › How-do-I-get-the-size-of-an-array-in-Java
How to get the size of an array in Java - Quora
Answer (1 of 6): Steps 1. Open the IDE. Open your IDE of choice, or write the code and execute it through the Command Prompt. 2. Copy and paste the following code: class Size { static int[] numbers = {1, 225, 231, 4, 675}; public static void main(String[] args) { int size = numbers.length...
🌐
Software Testing Help
softwaretestinghelp.com › home › java tutorial for beginners: 100+ hands-on java video tutorials › java array length tutorial with code examples
Java Array Length Tutorial With Code Examples
April 1, 2025 - Java doesn’t provide any method ... gives the length or size of the array. ... The number of elements in the array during declaration is called the size or length of the array....
🌐
Educative
educative.io › answers › how-to-resize-an-array-in-java
How to resize an array in Java
For example, if we have an array of size 3 and we want to resize it to size 6, we can do it as follows: ... Line 5: We declare an integer array of three elements, named oldArray , with values 1, 2, and 3. Line 6: We declare an integer 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.