Best practices and conventions evolve with time. The String class was made in Java 1.0, and the convention just hadn't been established. Answer from WrickyB on reddit.com
🌐
GeeksforGeeks
geeksforgeeks.org › java › file-length-method-in-java-with-examples
File length() method in Java with Examples - GeeksforGeeks
July 11, 2025 - Exception: This method throws Security Exception if the write access to the file is denied Below programs illustrate the use of the length() function: Example 1: The file "F:\\program.txt" is an existing file in F: Directory. ... // Java program to demonstrate // length() method of File Class import java.io.*; public class solution { public static void main(String args[]) { // Get the file File f = new File("F:\\program.txt"); // Get the length of the file System.out.println("length: " + f.length()); } }
🌐
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 Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... The length() method returns the length of a specified string.
🌐
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--

🌐
Mother Eff
mothereff.in › byte-counter
UTF-8 string length & byte counter
Pro tip: add http://mothereff.in/byte-counter#%s to the custom search engines / location bar shortcuts in your browser of choice. Whenever I want to check string length / byte count, I just enter len some string in my address bar.
🌐
Sololearn
sololearn.com › en › Discuss › 1177842 › in-java-array-why-we-are-using-length-to-get-numbers-or-to-check-ex-i-arrlength
In java array why we are using .length to get numbers or to check?? Ex: i <arr.length | Sololearn: Learn to code for FREE!
March 29, 2018 - .length is a mathod that returns the number of variable you have in your array. For exemple, in java you can declares an array using this form: int[] arr = new int[5] The above code means that you create an object that have 5 variables.
🌐
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. Example: JAVA ·
🌐
Tutorialspoint
tutorialspoint.com › home › java › java string length
Java String Length
September 1, 2008 - This method returns the the length of the sequence of characters represented by this object. import java.io.*; public class Test { public static void main(String args[]) { String Str1 = new String("Welcome to Tutorialspoint.com"); String Str2 ...
Find elsewhere
🌐
Mccue
javabook.mccue.dev › strings › length
Length - Modern Java
void main() { String fruit = "strawberry"; int numberOfChars = fruit.length(); // strawberry is 10 characters long IO.println( fruit + " is " + numberOfChars + " characters long" ); }
🌐
Tutorial Gateway
tutorialgateway.org › java-string-length-method
Java String length method
March 28, 2025 - The Java length Method is to find and return the length of the User specified string. The length is equal to the number of Unicode units in a string.
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.

🌐
Coderanch
coderanch.com › t › 411712 › java › Writing-length-method
Writing my own length() method (Beginning Java forum at Coderanch)
September 3, 2008 - Considering that you are not allowed to use the length() method from the String class, you are probably not allowed to use the length field from the byte array or the char array that can be returned either. Given these restrictions, taking an exception hit may be the only option. Regardless, I agree with EFH, this is a pretty silly exercise. Henry [ September 03, 2008: Message edited by: Henry Wong ] Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Find-Java-String-Length-Example-Tutorial
How do I find the Java String length?
The Java String class contains a length() method that returns the total number of characters a given String contains.
🌐
Vertex Academy
vertex-academy.com › tutorials › en › length-in-java
The Java length() method • Vertex Academy
March 28, 2018 - This is one of the articles from our Java Tutorial for Beginners. The length() method is used to find a String's length (the number of characters in a String).
🌐
LeetCode
leetcode.com › problems › merge-k-sorted-lists
Merge k Sorted Lists - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
🌐
HackMD
hackmd.io › @topics › S18kNripT
Java String length() Method - Scaler Topics - HackMD
March 14, 2024 - ```java public int length() { return ... { String word = "Hello"; int word_length= word.length(); // length() calculates the number of characters in the word System.out.println("The length of the word is " + word_length); } } ``` ...
🌐
Simplilearn
simplilearn.com › home › resources › software development › string length in java: determining a string’s length
String Length in Java: Determining a String’s Length
September 13, 2022 - Just like the name suggests, string length in java method returns the length of this string. But is that it? Learn about this method with some examples now.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Playwright
playwright.dev › assertions
Assertions | Playwright
Playwright includes test assertions in the form of expect function. To make an assertion, call expect(value) and choose a matcher that reflects the expectation. There are many generic matchers like toEqual, toContain, toBeTruthy that can be used to assert any conditions · Playwright also includes ...