Java String length() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Java, length() returns how many char values a string contains. "Hello".length() is 5, and the empty string "" has length 0. Spaces, tabs, and punctuation all count.

The valid indexes of a string run from 0 to length() - 1, so length() appears in almost every loop, bounds check, and validation rule. Note the parentheses: arrays use the field array.length, strings use the method text.length().

length() is an instance method of java.lang.String, available since JDK 1.0. Call it on a string, as in text.length(); calling it on a null reference throws NullPointerException. It only reads the text; the string itself is never changed.

Syntax

Call it with no arguments; the result is an int.

java
int size = text.length();
char last = text.charAt(text.length() - 1);
boolean tooLong = name.length() > 20;
CallWhat it does
"Java".length()4.
"".length()0.
"a b".length()3: the space counts.

length() counts UTF-16 code units. Most characters are one unit, but emoji and other characters above U+FFFF are two. Use codePointCount(0, length()) to count Unicode code points instead.

Library declarations
java
public int length();

Parameters

ParameterDescription
Nonelength() takes no parameters.

Return Value

Return ValueDescription
intThe number of chars in the string (0 or more).

Java Examples

Example 1 — Count the Characters of Different Strings

Spaces, tabs, and the empty string all follow the same rule.

java
public class LengthBasics {
    public static void main(String[] args) {
        System.out.println("Hello".length());
        System.out.println("".length());
        System.out.println("Hi there".length());
        System.out.println("Tab\tEnd".length());
    }
}

How It Works

  1. Letters only. "Hello" has five chars, and the empty string has none.
  2. Whitespace is a char too. "Hi there" counts its space: 2 + 1 + 5 = 8.
  3. An escape is one char. \t is written with two symbols in the source but stores a single tab char, so "Tab\tEnd" has length 7.

Example 2 — Check a Minimum Password Length

Reject passwords shorter than eight characters and report their length.

java
public class LengthPassword {
    public static void main(String[] args) {
        String[] passwords = {"secret", "correct horse", "12345678"};
        int min = 8;

        for (String p : passwords) {
            String verdict = p.length() >= min ? "ok" : "too short (" + p.length() + ")";
            System.out.println(p + " -> " + verdict);
        }
    }
}

How It Works

  1. Compare with a limit. p.length() >= min is the whole rule; min is 8.
  2. Short input. "secret" has 6 chars and fails, and the message includes the measured length.
  3. Exactly the limit passes. "correct horse" (13) and "12345678" (8) are both accepted.

Example 3 — Why an Emoji Has Length 2

Compare an accented word with a single smiley and count its code points.

java
public class LengthUnicode {
    public static void main(String[] args) {
        String cafe = "caf\u00E9";
        String smile = "\uD83D\uDE00";

        System.out.println("cafe length: " + cafe.length());
        System.out.println("smile length: " + smile.length());
        System.out.println("smile code points: " + smile.codePointCount(0, smile.length()));
    }
}

How It Works

  1. Accented letters in the BMP. The e with acute accent (U+00E9) is a single char, so the word has length 4.
  2. Emoji are surrogate pairs. The smiley U+1F600 needs two UTF-16 units, so length() reports 2 for one visible symbol.
  3. Count what readers see. codePointCount(0, length()) merges the pair and returns 1.

Frequently Asked Questions

The number of UTF-16 chars in the string. "Java".length() is 4 and "".length() is 0.
Arrays expose a final field named length; String is a class and provides the length() method. Mixing them up is a compile error.
Yes. Every char counts, including spaces, tabs, newlines, and punctuation.
Java strings use UTF-16. Characters above U+FFFF, such as most emoji, are stored as two chars. Use codePointCount() to count them once.
A NullPointerException is thrown. Check text != null first, or use a helper such as text == null || text.isEmpty().

Next: lines()

Split text into a Stream of lines (Java 11+).

lines() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful