The length is just text.length(). Checking for a \0 terminator is a C-ism. Java doesn't terminate strings with NUL.

🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-length-method-with-examples
Java String length() Method - GeeksforGeeks
December 23, 2024 - The length() method returns the number of characters present in the string.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
October 20, 2025 - Constructs a new String by decoding the specified subarray of bytes using the specified charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the subarray.
🌐
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--

🌐
Reddit
reddit.com › r/programminghorror › baeldung -- checking length of a string in java
r/programminghorror on Reddit: Baeldung -- Checking Length of a String in Java
October 31, 2021 - That's checking the length of an int or long, not a String. ... Edit: I found it. https://www.baeldung.com/java-number-of-digits-in-int
🌐
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 String length method includes the whitespace padding at the beginning and the end of the String. Quite often, when validating input or manipulating text, you want to eliminate leading and trailing whitespace. This can be achieved through the use of the Java String’s trim method.
Find elsewhere
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.string.length
String.Length Method (Java.Lang) | Microsoft Learn
Returns the length of this string. The length is equal to the number of Unicode code units in the string. Java documentation for java.lang.String.length().
🌐
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()); } }
🌐
Codekru
codekru.com › home › java string length() method
Java String length() method - Codekru
June 19, 2022 - java · length() method tells us about the size of the string. In this post, we will discuss the length() method of the String class in detail. Method declaration – public int length(). What does it do and return? It helps in returning the size of the string.
Top answer
1 of 7
188

Considering the String class' length method returns an int, the maximum length that would be returned by the method would be Integer.MAX_VALUE, which is 2^31 - 1 (or approximately 2 billion.)

In terms of lengths and indexing of arrays, (such as char[], which is probably the way the internal data representation is implemented for Strings), Chapter 10: Arrays of The Java Language Specification, Java SE 7 Edition says the following:

The variables contained in an array have no names; instead they are referenced by array access expressions that use nonnegative integer index values. These variables are called the components of the array. If an array has n components, we say n is the length of the array; the components of the array are referenced using integer indices from 0 to n - 1, inclusive.

Furthermore, the indexing must be by int values, as mentioned in Section 10.4:

Arrays must be indexed by int values;

Therefore, it appears that the limit is indeed 2^31 - 1, as that is the maximum value for a nonnegative int value.

However, there probably are going to be other limitations, such as the maximum allocatable size for an array.

2 of 7
32

java.io.DataInput.readUTF() and java.io.DataOutput.writeUTF(String) say that a String object is represented by two bytes of length information and the modified UTF-8 representation of every character in the string. This concludes that the length of String is limited by the number of bytes of the modified UTF-8 representation of the string when used with DataInput and DataOutput.

In addition, The specification of CONSTANT_Utf8_info found in the Java virtual machine specification defines the structure as follows.

CONSTANT_Utf8_info {
    u1 tag;
    u2 length;
    u1 bytes[length];
}

You can find that the size of 'length' is two bytes.

That the return type of a certain method (e.g. String.length()) is int does not always mean that its allowed maximum value is Integer.MAX_VALUE. Instead, in most cases, int is chosen just for performance reasons. The Java language specification says that integers whose size is smaller than that of int are converted to int before calculation (if my memory serves me correctly) and it is one reason to choose int when there is no special reason.

The maximum length at compilation time is at most 65536. Note again that the length is the number of bytes of the modified UTF-8 representation, not the number of characters in a String object.

String objects may be able to have much more characters at runtime. However, if you want to use String objects with DataInput and DataOutput interfaces, it is better to avoid using too long String objects. I found this limitation when I implemented Objective-C equivalents of DataInput.readUTF() and DataOutput.writeUTF(String).

🌐
Upgrad
upgrad.com › home › tutorials › software & tech › string length in java
String length() Method in Java – Syntax, Example & Use Cases
April 30, 2025 - Learn how to use the length() method in Java to find the number of characters in a string. Explore syntax, examples, and common use cases for better string handling.
🌐
Baeldung
baeldung.com › home › java › java string › java’s string.length() and string.getbytes().length
Java’s String.length() and String.getBytes().length | Baeldung
July 6, 2024 - String s = "beautiful"; assertEquals(9, s.length()); assertEquals(9, s.getBytes().length); When dealing with a string in Java, is it guaranteed that String.length() and String.getBytes().length always yield the same value? Next, let’s figure it out. The default character encoding or charset of the current JVM plays an important role in deciding the result of String.getBytes().length.
🌐
DataFlair
data-flair.training › blogs › java-string-length-method
Java String length() Method with Examples - DataFlair
July 18, 2024 - The length() method returns an int value, which is the number of Unicode code units in the string. This value ranges from 0 for an empty string to 2,147,483,647 for a string containing the maximum number of Unicode code units.
🌐
CodeAhoy
codeahoy.com › java › string-length
Java String Length() Method with Examples | CodeAhoy
January 26, 2020 - The String class contains many useful string methods. To find the length of a string in Java, we can use the length() method of the String class. This method is called on the string object whose length you want to find. It takes no parameters and returns the number of characters (length) as an int.
🌐
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...
🌐
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.
🌐
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.
🌐
JanBask Training
janbasktraining.com › community › java › length-and-length-in-java
length and length() in Java | JanBask Training Community
May 15, 2025 - Type: length is a field (array), while length() is a method (string). Syntax: Arrays use .length, Strings use .length().
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-length-method-example
Java String length() Method with examples
September 11, 2022 - This method counts the number of characters in a String including the white spaces and returns the count. ... This method returns an integer number which represents the number of characters (length) in a given string including white spaces.