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
A common example of the Java array length property being used in code is a program looping through all of the elements in an 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.
🌐
Codecademy
codecademy.com › docs › java › arrays › .length
Java | Arrays | .length | Codecademy
April 21, 2025 - Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more. 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 ...
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.

🌐
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 - This program is to find the minimum element in the array. import java.util.*; class Main { public static void main(String[] args) { int[] intArray = { 72,42,21,10,53,64 }; //int array System.out.pr
🌐
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. It gives the exact number of elements present in the array. Example: This example demonstrates how to find the length (size) of an array using the length property.
Published   July 12, 2025
🌐
HappyCoders.eu
happycoders.eu › java › array-length-in-java
Array Length in Java
June 12, 2025 - The length of an array is defined when it is initialized. The following code, for example, creates a string array of size four: String[] fruits = {"jujube", "apple", "boysenberry", "cherry"};Code language: Java (java)
Find elsewhere
🌐
Alvin Alexander
alvinalexander.com › java › java-array-length-example-length-method
A Java array length example | alvinalexander.com
The approach you take just depends on whether you want to use a arrayLength variable in your code, or if you just want to access the array length attribute (toppings.length). Either of these two Java array length approaches will print the following output:
🌐
Edureka
edureka.co › blog › array-length-in-java
Array Length In Java | Java Array Examples | Edureka
December 4, 2023 - It must be noted, that Java Array Object does not have a method to get its length. Often times, we are unaware of how the array object was created. For such programs, we use a function that receives an array, and prints the length. /** * An Example to find the Java Array Length using a function */ public class ArrayLengthJava { private static void printArrayLength(String[] myArray) { if (myArray == null) //to check whether the array is empty or not { System.out.println("The length of the array can't be determined."); } else { int arrayLength = myArray.length; System.out.println("The length of
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › arrays.html
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
Here the length of the array is determined by the number of values provided between braces and separated by commas. You can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of brackets, such as String[][] names. Each element, therefore, must be accessed by a corresponding number of index values. In the Java programming language, a multidimensional array is an array whose components are themselves arrays.
🌐
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.
🌐
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 - To understand better, go to the section 'Searching for the highest value in the array' of this article, where we discussed an example. A. There are different scenarios to find the length. For example, we have two options to use the length() method or the java length attribute.
🌐
W3Schools
w3schools.com › java › java_arrays.asp
Java Arrays
Arrays Loop Through an Array Real-Life Examples Multidimensional Arrays Code Challenge · Java Methods Java Method Challenge Java Method Parameters
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Java Array Length Example - Java Code Geeks
July 6, 2022 - The above example calculates the sum of all the elements of an integer array by traversing all the elements of the array in a for-loop and adding the element in the variable sum. The length property is obtained by array.length and it helps in determining the size of the array so as to provide the exit condition for the for-loop in Java.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › array length in java
Java Array Length with Examples & Syntax
October 26, 2025 - Explanation: We are accessing the length of the array cities, which contains 3 elements. Enhance your abilities through these best-in-class courses/certifications. upGrad’s AI-Powered Full Stack Development Course by IIITB · upGrad’s Professional Certificate Program in Cloud Computing and DevOps ... Java offers a few ways to determine array size depending on what you're trying to do.
🌐
CodeAhoy
codeahoy.com › java › array-length
Array Length in Java with Examples | CodeAhoy
January 26, 2020 - public class ArrayLength { public static void main(String[] args) { // Let's define some arrays int[] arrNumbers = {1,2,3,4}; char[] arrCharacters = {'a', 'b', 'c'}; String[] arrStrings = {"java", "c++", "c", "react", "spring"}; Object[] arrObj = new Object[10]; // new array with uninitialized elements // Print length of arrays System.out.println("arrNumbers.length is: " + arrNumbers.length); System.out.println("arrCharacters.length is: " + arrCharacters.length); System.out.println("arrStrings.length is: " + arrStrings.length); System.out.println("arrObj.length is: " + arrObj.length); } }
🌐
iO Flood
ioflood.com › blog › java-array-length
Java Array Length: How To Get the Array Size in Java
February 27, 2024 - In Java, you can use the .length property of an array to find its length by using the syntax, int length = array.length;. It’s a simple and straightforward way to determine the size of your 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?
The following example illustrates ... { int[] aray = new int[5]; // declaring array of size 5 // initializing the array aray[0] = 11; aray[1] = 21; aray[2] = 13; aray[3] = 23; aray[4] = 30; // printing the elements of array System.out.println("Elements of the given array: "); ...
🌐
Medium
medium.com › javarevisited › understanding-java-arrays-from-basics-to-length-and-limits-9c30f749f2fd
Understanding Java Arrays: From Basics to Length and Limits | by sajith dilshan | Javarevisited | Medium
August 25, 2025 - Array .length vs String .length(): Arrays → .length, Strings → .length(). Updating: Array size is fixed, but you can always replace elements inside. Loops: Arrays pair perfectly with loops for efficient data handling. Once you’re comfortable with arrays, you’ll find it much easier to transition to flexible collections like ArrayList, which can grow and shrink as needed. ... Follow me for more bite-sized Java tips and dev insights.