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
๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-string-length
Java String length() Method - javatpoint
Java String length() method with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split, string length in java etc.
๐ŸŒ
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.
Discussions

arrays - length and length() in Java - Stack Overflow
Why do we have the length of an array as an attribute, array.length, and for String we have a method, str.length()? Is there some reason? More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Difference between size and length methods? - Stack Overflow
What is the difference between .size() and .length ? Is .size() only for arraylists and .length only for arrays? More on stackoverflow.com
๐ŸŒ stackoverflow.com
x.length, x.length(), or length(x)?
x.length is a proprety not a function. x.length() is a class method that can be be called on any instace of that class. length(x) is a static method that accepts x as an argument. More on reddit.com
๐ŸŒ r/learnprogramming
9
3
September 16, 2021
Working of the length function
it doesn't apply to every language as some languages have different kind of "arrays". in java everything is an object including arrays, so they simply keep a "length" attribute to the object and maintain it pretty easily. and finding the length differ from one language to the other, i would say without using said attribute you can always iterate it with try and catch, there might be a more elegant way to do so but its been a while since i played around with java More on reddit.com
๐ŸŒ r/learnprogramming
11
1
October 12, 2020
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ java_string_length.htm
Java - String length() Method
Java Vs. C++ ... This method returns the length of this string. The length is equal to the number of 16-bit Unicode characters in the string.
๐ŸŒ
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 main(String[] args) { // Here array is the array name of int type int[] array = new int[4]; System.out.println("The size of the array is " + array.length); // Here str is a string object String str = "GeeksforGeeks"; System.out.println("The size of the String is " + str.length()); } }
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_string_length.asp
Java String length() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... The length() method returns the length of a specified string.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ length-java
.Length() Java: A Guide for Beginners to Experts
March 4, 2024 - In Java, .length is used to get the length of an array or a string, with the syntax int length = str.length();. Itโ€™s a property that helps you determine the size of your arrays and strings, making it a handy tool in your Java programming toolkit.
Find elsewhere
๐ŸŒ
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?
The key difference between Javaโ€™s length variable and Javaโ€™s length() method is that the Java length variable describes the size of an array, while Javaโ€™s length() method tells you how many characters a text String contains.
๐ŸŒ
JanBask Training
janbasktraining.com โ€บ community โ€บ java โ€บ length-and-length-in-java
length and length() in Java | JanBask Training Community
May 15, 2025 - int[] numbers = {1, 2, 3, 4}; System.out.println(numbers.length); // Output: 4 ... Used for strings. It's a method that returns the number of characters in the string. Parentheses are needed because itโ€™s a method call.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ java โ€บ strings โ€บ .length()
Java | Strings | .length() | Codecademy
June 22, 2025 - In Java, the .length() method returns the length or number of characters in a string. Itโ€™s a built-in method that comes with the String class.
๐ŸŒ
Wikitechy
wikitechy.com โ€บ length-vs-length-in-java
length vs length() in Java - Wikitechy
December 21, 2020 - To directly accesses a field member of array we will use .length; whereas .length() invokes a way to access a field member. ... // Java program to illustrate the // concept of length // and length() public class Test { public static void main(String[] args) { // Here array is the array name of int type int[] array = new int[4]; System.out.println("The size of the array is " + array.length); // Here str is a string object String str = "wikitechy"; System.out.println("The size of the String is " + str.length()); } }
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ java โ€บ java string length() method with examples
Java String length() Method With Examples
April 1, 2025 - When you are going to compute the length of a String which has whitespace, special characters, etc., then they will be treated as a character. Every single character has size = 1. Q #3) How to create a String of specified size in Java?
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ string length in java
String length() Method in Java โ€“ Syntax, Example & Use Cases
April 30, 2025 - Explanation:The string "Programming" has 11 characters, while "Java" has only 4 characters. So, the program correctly identifies the longer string. ... User Input Validation: Check if passwords meet minimum character requirements.
๐ŸŒ
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.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ length-of-string-java
How to Find Length of String Java | Effective Methods
July 8, 2024 - Learn how to determine the length of string in Java using length() and other efficient methods. Understand java string length syntax with examples.
๐ŸŒ
Jaro Education
jaroeducation.com โ€บ home โ€บ blog โ€บ how to find the length of a string in java
Java String Length Method: Everything You Need to Know
2 days ago - This method returns the total number of characters in a string, including spaces and special characters. Here are the steps to find the Java String Length Method.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-find-the-length-of-a-string-in-java
How to find the length of a string in Java
To calculate the length of a string in Java, you can use an inbuilt length() method of the Java string class.
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ strings in java โ€บ string length() method
String length() Method
May 30, 2022 - public class StringLength { public static void main(String args[]) { String alphabetsWithSpaces = "A B C"; String digitsWithSpaces = "1 2 3"; String specialCharsWithSpaces = "! @ $"; String allChars = "Z X 3 $$$ ^^^ * )("; String sentence = "Hey, it's finally summers!"; System.out.println(alphabetsWithSpaces + " length = " + alphabetsWithSpaces.length()); System.out.println(digitsWithSpaces + " length = " + digitsWithSpaces.length()); System.out.println(specialCharsWithSpaces + " length = " + specialCharsWithSpaces.length()); System.out.println(allChars + " length = " + allChars.length()); System.out.println(sentence + " length = " + sentence.length()); } }
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ library โ€บ string โ€บ length
Java String length()
The length is equal to the number of characters (code units) in the string. class Main { public static void main(String[] args) { String str1 = "Java"; String str2 = ""; System.out.println(str1.length()); // 4 System.out.println("Java".length()); // 4 // length of empty string System.out.p...