Java String length() Method
In Java, the length() method of the String class is used to find the number of characters in a string. It returns an int value representing the total count of Unicode code units (characters) in the string, including letters, digits, spaces, punctuation, and special characters.
Syntax:
string.length()Return Type:
intExamples:
String greeting = "Hello, World!"; System.out.println(greeting.length()); // Output: 13Key Points:
Counts all characters, including whitespace and special symbols.
The maximum length of a string is 2,147,483,647 (2^31 - 1), limited by the
intdata type.Efficient: operates in O(1) time complexity, as it returns a pre-stored length value.
Not the same as
array.length(arrays use.lengthwithout parentheses).Can be used with
StringBuilderandStringBufferclasses as well.
⚠️ Note: Calling
length()on anullstring will throw aNullPointerException. Always ensure the string is not null before calling the method.
Why is it String.length() instead of String.getLength()? How to name my own length method?
how to write java string length method code? - Stack Overflow
String's Maximum length in Java - calling length() method - Stack Overflow
[Java] How is the running time of string concatenation O(n^2)?
How do you find the length of a String in Java?
How do you find the length of a String array in Java?
What is the difference between String length() and array length in Java?
Videos
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--
The length is just text.length(). Checking for a \0 terminator is a C-ism. Java doesn't terminate strings with NUL.
As stated in this SO,
Strings in Java do NOT have a NULL terminator as in C, so you need to use the length() method to find out how long a string is.
Therefore you can change the condition to
while (i < text.length())
(If your goal really is to get the length of a string, your function therefore is redundant, as it is already built in)
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
ncomponents, we saynis the length of the array; the components of the array are referenced using integer indices from0ton - 1, inclusive.
Furthermore, the indexing must be by int values, as mentioned in Section 10.4:
Arrays must be indexed by
intvalues;
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.
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).