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
134

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
This is a built-in Java property, and does not belong to the Java Arrays Class. Note: The length property must not be mistaken with the length() method that is used for Strings.
Discussions

How to find out the length of an array in Java
3.8K votes, 183 comments. 4.7M subscribers in the ProgrammerHumor community. For anything funny related to programming and software development. More on reddit.com
🌐 r/ProgrammerHumor
183
3830
November 12, 2017
Why is getting the length of an array O(1) and not O(n)?
Most implementations store the length of an array in the object itself. Whenever you insert/remove it gets updated. Thus you're not iterating to get the length you're just getting a value. More on reddit.com
🌐 r/learnprogramming
45
11
November 7, 2022
How to check the length of the array?
Here, read this. Get familiar with everything about arrays, you'll be seeing them a lot. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html More on reddit.com
🌐 r/javahelp
6
4
September 19, 2020
People also ask

Is .length a method or a property in Java arrays?
In Java, .length is a property of arrays, not a method. Unlike String.length() or ArrayList.size(), you don’t use parentheses. It directly returns the total number of elements the array can hold. In Java, .length is a property of arrays, not a method. Unlike String.length() or ArrayList.size(), you don’t use parentheses. It directly returns the total number of elements the array can hold.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › array length in java
Java Array Length with Examples & Syntax
Can we get array length using reflection in Java?
Yes, you can use the Array.getLength() method from the java.lang.reflect package. It’s useful when dealing with arrays generically without knowing the type at compile time. It's commonly used in frameworks and utility libraries.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › array length in java
Java Array Length with Examples & Syntax
Can array length in Java be changed after initialization?
No, array length in Java is fixed once the array is created. If you need a resizable structure, consider using collections like ArrayList. To change size, you must create a new array and copy data manually or use Arrays.copyOf().
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › array length in java
Java Array Length with Examples & Syntax
🌐
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 size or length count of an array in Java includes both null and non-null characters. Unlike the String and ArrayList, Java arrays do not have a size() or length() method, only a length property.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › array length in java
Java Array Length with Examples & Syntax
June 4, 2025 - JavaScript, where array length can change dynamically, Java arrays have a fixed length once declared. ... Explanation: The array marks holds 4 elements, so marks.length returns 4. Note that length is a property, not a method.
🌐
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
🌐
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?
The Java array length property does not have round brackets after the field. The Java String length() method does. Confusion here will cause compile time errors, so be careful. Use the length property to find the size of a Java array.
Find elsewhere
🌐
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 - Answer: The length function in Java returns the number of characters present in a string object. ... Answer: It depends on whether you want to get the length of the string or an array. If it’s a string then using length() method will give you the number of characters in the string. If it is an array, you can use the ‘length’ property of the array to find the number of elements in the array.
🌐
Codecademy
codecademy.com › docs › java › arrays › .length
Java | Arrays | .length | Codecademy
April 21, 2025 - 244 total contributions · 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.
🌐
CodeAhoy
codeahoy.com › java › array-length
Array Length in Java with Examples | CodeAhoy
January 26, 2020 - As an example, if you have an array of int[], you can find its length (number of elements) by using the length field of the array. int[] arrNumbers = {1,2,3,4}; int length = arrNumbers.length; System.out.println("length is: " + length); The code above will print 4 (output below) because there ...
🌐
Study.com
study.com › business courses › java programming tutorial & training
Array Lengths in Java | Study.com
You'll learn how Java represents and stores arrays of data, how to access array elements and determine the number of elements (i.e., the length) of an array.
🌐
FITA Academy
fita.in › array-length-in-java
Length attribute in Java: Finding the size of an array | FITA Academy
Prashanth
In this tutorial, we will look into how to find the length of an array and few useful practice programs for you where you will need to use the length of the array. I joined in FITA to pursue Expertise in Digital Marketing. The training they had given to me is wonderful and they taught more tools and working knowledge in digital marketing. Efficient trainer. He gave me many tips and tricks on how to handle and survive in the field of digital marketing. Overall its a best place to learn Digital marketing course. FITA is a leading Training and Placement company managed by IT veterans with more th
Rating: 5 ​
Call   9345045466
Address   Plot No 7, 2nd floor, Vadivelan Nagar, Velachery Main Road, Velachery, 600042, Chennai
🌐
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.
🌐
Webzeto
webzeto.com › home › programing › how to find the length of an array in java
How to Find the Length of an Array in Java - Webzeto
March 27, 2025 - In Java, you can easily find the length of an array using the length property or the getLength() method for multi-dimensional arrays. This article will guide you through different ways to find the length of an array.
🌐
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.
🌐
Delft Stack
delftstack.com › home › howto › java › java get array length
How to Get Length of Array in Java | Delft Stack
March 11, 2025 - By accessing the length property of the array, we store the value in the variable length. Finally, we print out the length, which in this case is 5. This method is efficient and simple, making it the most commonly used way to get the length ...
🌐
iO Flood
ioflood.com › blog › java-array-length
Java Array Length: How To Get the Array Size in Java
February 27, 2024 - The ‘length’ property offers a simple and effective way to determine the size of an array in Java. It’s readily available and doesn’t require any additional method calls.
🌐
iO Flood
ioflood.com › blog › length-java
.Length() Java: A Guide for Beginners to Experts
March 4, 2024 - While .length is a powerful tool for arrays and strings in Java, it’s not the only way to determine the length or size. As you become more proficient in Java, you’ll encounter alternative methods, especially when working with collections.
🌐
Daily Java Concept
dailyjavaconcept.com › home › data structure › arrays › find the length of array in java
Find the length of Array in Java - Daily Java Concept
November 17, 2019 - Find the length of Array in Java. That’s a very basic but very interesting question. whenever you start coding with the array. Like you need to print the array elements, search an element in the array or modify the element in the array. In all cases, you need to know the size of the array, because array size needs to be used to iterate over the array.