Java String chars() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Java, chars() returns an IntStream with one int per char of the string, in order. Each value is the UTF-16 code of that char, for example 72 for 'H'.

It lets you count, filter, and transform characters with the Stream API instead of a manual loop: text.chars().filter(Character::isDigit).count(). The stream yields int values, so cast with (char) c when you want to print the character itself.

chars() is an instance method of java.lang.String, usable since Java 8 (inherited from CharSequence; String overrides it since Java 9). Call it on a string, as in text.chars(); calling it on a null reference throws NullPointerException. It only reads the text; the string itself is never changed. Requires Java 8 or newer.

Syntax

Call it with no arguments and chain stream operations.

java
long digits = text.chars().filter(Character::isDigit).count();
int sum     = text.chars().sum();
text.chars().forEach(c -> System.out.println((char) c));
CallWhat it does
"abc".chars().count()3.
"abc".chars().max().getAsInt()99, the code of 'c'.
"a1b2".chars().filter(Character::isDigit).count()2.

chars() works on UTF-16 units: an emoji such as U+1F600 arrives as two surrogate values. Use codePoints() to get one value per real Unicode character.

Library declarations
java
public IntStream chars();

Parameters

ParameterDescription
Nonechars() takes no parameters.

Return Value

Return ValueDescription
IntStreamThe char values of the string as ints, from index 0 to length() - 1.

Java Examples

Example 1 — Count Digits, Capitals, and Spaces

Use one filter per question about the characters of an order message.

java
public class CharsCountDigits {
    public static void main(String[] args) {
        String order = "Order 66 shipped in 2026";

        long digits = order.chars().filter(Character::isDigit).count();
        long upper = order.chars().filter(Character::isUpperCase).count();
        long spaces = order.chars().filter(c -> c == ' ').count();

        System.out.println("digits: " + digits);
        System.out.println("uppercase: " + upper);
        System.out.println("spaces: " + spaces);
    }
}

How It Works

  1. A fresh stream per question. Each order.chars() call creates a new IntStream, because a stream cannot be reused after count().
  2. Method references as filters. Character::isDigit keeps the digits of "66" and "2026" (6 in total); Character::isUpperCase keeps only the 'O'.
  3. Compare ints with chars. In c -> c == ' ' the char literal widens to 32, and the four gaps between the five words match.

Example 2 — See the int Values Behind Each char

Print every value next to its character, then add them all up.

java
public class CharsPrintCodes {
    public static void main(String[] args) {
        String word = "Hi!";

        word.chars().forEach(c -> System.out.println(c + " -> " + (char) c));
        System.out.println("sum: " + word.chars().sum());
    }
}

How It Works

  1. Streams deliver ints. c holds 72, 105, and 33, the UTF-16 codes of 'H', 'i', and '!'.
  2. Cast to see the character. (char) c turns each number back into the letter it stands for.
  3. Numeric operations come free. IntStream.sum() adds the codes: 72 + 105 + 33 = 210.

Example 3 — List the Distinct Letters of a Word

Remove duplicates, sort, and join the characters back into text.

java
import java.util.stream.Collectors;

public class CharsDistinct {
    public static void main(String[] args) {
        String word = "mississippi";

        String unique = word.chars()
                .distinct()
                .sorted()
                .mapToObj(c -> String.valueOf((char) c))
                .collect(Collectors.joining(","));

        System.out.println("unique letters: " + unique);
        System.out.println("count of s: " + word.chars().filter(c -> c == 's').count());
    }
}

How It Works

  1. Drop repeats, then order. distinct() leaves the codes for m, i, s, p; sorted() puts them in numeric (alphabetical) order.
  2. Back to strings. mapToObj(c -> String.valueOf((char) c)) turns each int into a one-letter String so Collectors.joining(",") can glue them.
  3. A second pass. A new chars() stream counts the four 's' characters in "mississippi".

Frequently Asked Questions

An IntStream with one int per char of the string. Each value is the UTF-16 code of the character.
Since Java 8, through the default method of the CharSequence interface. String has its own optimized override since Java 9; calling code is the same on both.
Cast each value: chars().forEach(c -> System.out.println((char) c)), or use mapToObj(c -> (char) c).
chars() gives UTF-16 units, so an emoji appears as two values. codePoints() gives one value per Unicode character.
No. A Java stream can be consumed only once. Call chars() again for every new pipeline.

Next: codePointAt()

Read the full Unicode code point that starts at an index.

codePointAt() 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