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
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-array-size-explained-by-example
Java array size, length and loop examples
To determine the size of a Java array, query its length property. Here is an example of how to access the size of a Java array in code:
🌐
Codecademy
codecademy.com › docs › java › arrays › .length
Java | Arrays | .length | Codecademy
April 21, 2025 - Beginner Friendly.Beginner Friendly17 hours17 hours ... The .length property returns an integer value that represents the number of elements in the array. This example uses the .length property to determine the array length: ... This example ...
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.

🌐
W3Schools
w3schools.com › java › ref_arrays_length.asp
Java Array length Property
How Tos Add Two Numbers Swap Two ... Number ArrayList Loop HashMap Loop Loop Through an Enum ... assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String ...
🌐
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 - As already mentioned, you can iterate through an array using the length attribute. The loop for this will iterate through all the elements one by one till (length-1) the element is reached (since arrays start from 0).
🌐
Dot Net Perls
dotnetperls.com › array-length-java
Java - Array Length - Dot Net Perls
The Java runtime applies special optimizations in loops. public class Program { public static void main(String[] args) { int[] array = new int[ 1000]; int length = array.length; long t1 = System.currentTimeMillis(); // Version 1: check array length. for (int i =
🌐
Edureka
edureka.co › blog › array-length-in-java
Array Length In Java | Java Array Examples | Edureka
December 4, 2023 - In the following example, we use the length of the array to loop through all the elements and to determine whether the specific value is present. /** * An Example that uses Java Array Length to check if the array contains a * specific value. */ public class ArrayLengthJava { private static boolean arrayContainsValue(String[] myArray, String lookForValue) { if (myArray != null) { int arrayLength = myArray.length; for (int i = 0; i <= arrayLength - 1; i++) { String value = myArray[i]; if (value.equals(lookForValue)) { return true; } } } return false; } public static void main(String[] args) { String[] JavaArray = { "I", "Love", "Music" }; System.out.println(arrayContainsValue(JavaArray, "Love")); System.out.println(arrayContainsValue(JavaArray, "Guitar")); } }
Find elsewhere
🌐
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.
🌐
Scaler
scaler.com › home › topics › how to find array length in java
How to Find Array Length in Java - Scaler Topics
April 4, 2024 - In the method searchValue, we used a for loop to cycle over the array and look for the supplied name. The function returns true once the name has been located. If the name is not present or the array is full, the method returns false. We can also utilize the length attribute of Java arrays to ...
🌐
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
Example: This example, demonstrates how to use for each loop to calculate the length of an array. Java ·
Published   July 12, 2025
🌐
Quora
quora.com › When-should-I-use-array-length-vs-array-length-1-in-a-for-loop
When should I use array.length vs array.length-1 in a for loop? - Quora
Answer (1 of 6): In languages like C, C++, C#, Java, counting starts from 0. So if you have N elements in a list or array, you should never try to get arr[N], because then you read one past the end. If you write your for loop like this: [code]int length = arr.length; for (int i = 0; i
🌐
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 - It can also be used for searching for a value in an array. You need to use a loop that will iterate through all the elements in the array one after the other until it finds the searched element.
🌐
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?
This means that the first element in an array is at index zero. However, the Java array length does not start counting at zero. When you use the length property to find the size of an array that contains five elements, the value returned for the Java array length is five, not four.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › array length in java
Java Array Length with Examples & Syntax
October 26, 2025 - Using .length on ArrayList Use .size() for lists, not .length. Accessing index beyond length - 1 This leads to ArrayIndexOutOfBoundsException. ... Array length helps loop through elements safely, preventing out-of-bound errors during iteration.
🌐
iO Flood
ioflood.com › blog › java-array-length
Java Array Length: How To Get the Array Size in Java
February 27, 2024 - In this example, we use the ‘length’ property (myArray.length) as the condition in the for loop. This ensures that the loop iterates exactly as many times as there are elements in the array, preventing an ‘ArrayIndexOutOfBoundsException’.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › arrays.html
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
In a real-world programming situation, you would probably use one of the supported looping constructs to iterate through each element of the array, rather than write each line individually as in the preceding example. However, the example clearly illustrates the array syntax. You will learn about the various looping constructs (for, while, and do-while) in the Control Flow section.
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-in-java
Arrays in Java - GeeksforGeeks
In Java, arrays can be easily traversed using a loop where the loop variable runs from 0 to array.length - 1. Java · class GFG{ public static void main(String[] args){ int[] arr = {2, 4, 8, 12, 16}; // Traversing and printing array for (int ...
Published   December 21, 2016
🌐
JetBrains
jetbrains.com › help › inspectopedia › ArrayLengthInLoopCondition.html
Array.length in loop condition | Inspectopedia Documentation
Reports accesses to the .length property of an array in the condition part of a loop statement. In highly resource constrained environments, such calls may have adverse performance implications. This inspection is intended for Java ME and other highly resource constrained environments.