They're two completely different things.

.length is a property on arrays. That isn't a method call.

.length() is a method call on String.

You're seeing both because first, you're iterating over the length of the array. The contents of the array are String, and you want to add up all of their lengths, so you then call the length on each individual String in the array.

Answer from Makoto on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › length-vs-length-java
length vs length() in Java - GeeksforGeeks
January 4, 2025 - 3. To directly access a field member of an array we can use .length; whereas .length() invokes a method to access a field member. ... // Java program to illustrate the // concept of length // and length() public class Test { public static void ...
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-length-vs-length-Whats-the-difference
Java length vs length(): What's the difference?
They key difference between Java length and length() constructs is that the method works with the String class, while the property works with arrays. This subtle difference often confuses new developers who are just getting used to Java syntax.
🌐
TutorialsPoint
tutorialspoint.com › what-are-the-differences-between-length-and-length-in-java
What are the differences between length and length () in Java?\\n
However, they are used with different Java objects. The length is an instance variable used for arrays, whereas length() is a method used with String objects.
🌐
Reddit
reddit.com › r/learnprogramming › x.length, x.length(), or length(x)?
r/learnprogramming on Reddit: x.length, x.length(), or length(x)?
September 16, 2021 -

In Java, there are functions that end with parentheses and there are some that don't, like x.length which is only used for arrays and x.length() only for strings. In Ruby, this is exactly the opposite. In JavaScript, x.length is used for both arrays and strings, so without parentheses, but then it's x.toUpperCase() which ends with parentheses...

And then in Python, it's len(x) (it could very well have been length(x)), where the variable is put inside the parenthesis as parameter to get the length of a string/array, but then it's x.upper() which again puts the variable outside of parentheses...

All languages aforementioned are objected oriented, but have their own way of calling upon the length functionality, which is super confusing. Is there a logic to why it's x.function, x.function(), or function(x)?

🌐
Coderanch
coderanch.com › t › 599411 › java › length-length
.length and .length() what (Beginning Java forum at Coderanch)
In the case of arrays, 'length' is a class variable that holds the number of indices in the array instance. In the case of Strings, length() is a method (not a variable) that returns the length of a String.
🌐
Interview Kickstart
interviewkickstart.com › home › blogs › learn › length vs. length method in java
Length vs. Length Method in Java | Interview Kickstart
December 18, 2025 - In Java, the number of elements an array can hold, or the size of the array, is called its length.
Address   4701 Patrick Henry Dr Bldg 25, 95054, Santa Clara
(4.7)
🌐
YouTube
youtube.com › cameron mckenzie
Difference between length() and length() in Java - YouTube
Ever wonder what the difference between Java length and length() properties and methods are? Well, the key is that the Java length property is used with arra...
Published   June 9, 2022
Views   7K
Find elsewhere
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.

🌐
Sololearn
sololearn.com › en › Discuss › 71774 › length-or-length-
length or length() ? | Sololearn: Learn to code for FREE!
That is to say: String string = "abc"; string.length(); // 3 List<int> list = Arrays.asList(1,2,3); list.length(); // 3 but! int[] array = {1,2,3}; array.length; // 3 array.length(); // Throws an error! It is what it is, I don't think there is a particular reason for why they did it like that. ... String is not actually a variable, but an abstract class in java.
🌐
Sololearn
sololearn.com › en › Discuss › 1915953 › what-is-the-difference-between-length-and-length
What is the difference between length and length() | Sololearn: Learn to code for FREE!
java · 3rd Aug 2019, 3:32 PM · Prataparao Sai Vamsi · 2 Answers · Answer · + 8 · length is a property while length() is a method. For classes that have some mention of a length, some have it as a property while some have it as a function ...
🌐
Hashnode
wottersheep.hashnode.dev › length-vs-length-in-java
length vs length() In Java - Untitled Publication - Hashnode
December 12, 2022 - The .length is an instance variable of an array in Java whereas length() is a method of String class.
🌐
Reddit
reddit.com › r/learnjava › why is it string.length() instead of string.getlength()? how to name my own length method?
r/learnjava on Reddit: Why is it String.length() instead of String.getLength()? How to name my own length method?
September 13, 2023 -

Hi, I've been trying to find a name for my method that returns the length of an object. A quick google search showed that it's conventional to name getters getX(). However, when I investigated the first standard class that came to mind - String, I found it having the length() method instead of expected getLength(). Why is that? How should I name a length method in my own class then?

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#length--

🌐
JanBask Training
janbasktraining.com › community › java › length-and-length-in-java
length and length() in Java | JanBask Training Community
May 15, 2025 - No Overlap: You can’t use .length() on arrays or .length on strings—it’ll cause a compile-time error. ... Keeping this distinction in mind will help you avoid common syntax errors and write more accurate Java code.
🌐
Quora
quora.com › What-is-the-difference-between-length-method-and-length-keyword-in-Java
What is the difference between length() method and length keyword in Java? - Quora
Answer (1 of 5): length : length is a final variable applicable for arrays.Length variable represents the size of the array. Ex: int x[]= new int[6]; System.out.println(x.length); //this code will be executed Answer:6// System.out.println(x...
🌐
Blogger
androidcodingworld.blogspot.com › 2017 › 09 › length-vs-length-in-java.html
AndroidCodingWorld: length vs length() in Java
Sample Code: public class Test { public static void main(String[] args) { int[] array = new int[4]; System.out.println("The size of the array is " + array.length); String str = "GeeksforGeeks"; System.out.println("The size of the String is " + str.length()); } } Output: The size of the array is 4 The size of the String is 13 ... Location: Plot. 49, Institutional Area, Prem Puri, Sector 32, Gurugram, Haryana 122018, India ... Check Whether a number is Duck Number or not java ...
🌐
JanbaskTraining
janbasktraining.com › home › difference between array length vs string length () function in java
Difference between Array Length vs String Length () Function in Java
August 8, 2018 - As we have stated in the above-section ... array. A Java object does not have length method, while arrays in Java have the final length field that contains the total number of characters, stored in that particular array...
🌐
Quora
quora.com › What-is-the-difference-between-length-and-size-in-Java
What is the difference between length and size in Java? - Quora
Answer (1 of 4): * In java size ... is contain by collection (not the capacity). * where as .length is a field which works with arrays and gives capacity of arrays. * also length is a m......