Java String.length() Method

Last Updated : 7 Aug 2026

In Java, String.length() method yields the total number of characters (or Unicode code units) in the specified string. It is crucial to remember that the length provided by this method may not match the length that humans perceive, particularly when working with strings that contain characters from multi-byte encoding languages like UTF-8.

Syntax:

Specified by: Length in CharSequence interface.

Returns: Length of the given string. In other words, the total number of characters present in the string.

Internal Implementation

The String class internally uses a char[] array to store the characters. The length variable of the array is used to find the total number of elements present in the array. Since the Java String class uses this char[] array internally.

Java String.length() Method

Example: Finding Length of Given Strings

Java

Compile and Run

Output:

string length is: 10
string length is: 6

Explanation

In the above program, two strings, "TpointTech" and "python", are stored in variables s1 and s2 respectively. The length() method is called on each to find the length (number of characters). The output shows that s1 has a length of 10 and s2 has a length of 6.

Example: Checking and Finding Length of String

Java

Compile and Run

Output:

String is not empty and length is: 10
String is empty now: 0

Explanation

In the above example, the string str is initialized to "TpointTech", and the code checks if its length is greater than 0 or not. Since it prints that the string is not empty along with its length.

Then, str is updated to an empty string "", and the code checks if its length is 0. As the condition is true, it prints that the string is now empty.

Example: Reversing a String Using the String.length() Method

Java

Compile and Run

Output:

Reverse of the string: 'Welcome To TpointTech' is
tniopTavaJ oT emocleW

Explanation

In the above program, we have defined and initialized a string in str variable. The length method determines the length of the given string. We have used a for loop that iterates over the string. For reversing the string we have used str.charAt(str.length() - i - 1). This technique combines the length() method with charAt() to efficiently reverse the string.

Example: Counting the White Spaces

Java

Compile and Run

Output:

In the string: ' Welcome To TpointTech '
Total number of whitespaces present are: 4

Explanation

The String.length() method can also be used to find only the white spaces present in the string. It first calculates the length of the string, including all spaces. Then it removes all spaces using the str.replace(" ", "") method and calculates the new length. By subtracting the new length from the original one, the code determines the total number of whitespace characters.

Important Points to Remember

  • Purpose: The length() method returns the total number of characters in a string. It includes letters, digits, spaces, and special characters.
  • Time Complexity: The method runs in constant time – O(1) – because it returns a pre-stored value from the char[] array used internally.
  • Includes All Characters: All characters are counted – even whitespaces, symbols, and escape characters. It does not ignore any visible or invisible characters.
  • Returns 0 for Empty Strings: If the string is empty (for example, ""), length() returns 0.
  • Not Based on Memory Size or Bytes: It does not return the number of bytes or memory size. It returns the number of Unicode characters, not the byte size, in encodings like UTF-8.
  • Used for Other Java Classes: It can also be used for StringBuilder and StringBuffer