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
Unlike the String and ArrayList, Java arrays do not have a size() or length() method, only a length property. 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:
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.

🌐
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
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › arrays.html
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
See Java Language Changes for a ... in Java SE 9 and subsequent releases. See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases. An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You have seen an example of arrays ...
🌐
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.
🌐
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)
🌐
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
🌐
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 ...
Find elsewhere
🌐
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: "); ...
🌐
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.
🌐
W3Schools
w3schools.com › java › java_arrays.asp
Java Arrays
Use new with a size when you want to create an empty array and fill it later. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript ...
🌐
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)
Once you create the 'house' with ... reason "int[2] array;" is wrong is because the reference doesn't know about the size of the array... it just needs to know it will point to an array that holds 'int's. There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors ... One more think I observe, When a programmer has a strong background of C and C++, the code : int arr[5]; seems to be ...
🌐
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 - in this case prints each element from another predefined Fibonacci sequence stored in fibArray. While it may seem limiting at first glance that once defined these sizes cannot change—this rigidity ensures stability and predictability within your applications compared with dynamically sized structures found elsewhere like Python lists or Javascript Arrays.
🌐
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 collections like ArrayList.
🌐
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.
🌐
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 - So, if you want to find the length of characters in a string, you need to use the length() method, whereas if you want to find the length or size of an array, you must use the length attribute of Java.
🌐
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.
🌐
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 - There is no length property in the ArrayList but the number of objects or elements in the ArrayList is given by size () method. Q #2) What is the difference between length and length() in Java?
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › reflect › Array.html
Array (Java Platform SE 8 )
October 20, 2025 - The Array class provides static methods to dynamically create and access Java arrays. Array permits widening conversions to occur during a get or set operation, but throws an IllegalArgumentException if a narrowing conversion would occur. clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait · public static Object newInstance(Class<?> componentType, int length) throws NegativeArraySizeException