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));
Call
What 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
Parameter
Description
None
chars() takes no parameters.
Return Value
Return Value
Description
IntStream
The 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);
}
}
Output
digits: 6
uppercase: 1
spaces: 4
How It Works
A fresh stream per question. Each order.chars() call creates a new IntStream, because a stream cannot be reused after count().
Method references as filters.Character::isDigit keeps the digits of "66" and "2026" (6 in total); Character::isUpperCase keeps only the 'O'.
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());
}
}
Output
72 -> H
105 -> i
33 -> !
sum: 210
How It Works
Streams deliver ints.c holds 72, 105, and 33, the UTF-16 codes of 'H', 'i', and '!'.
Cast to see the character.(char) c turns each number back into the letter it stands for.
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());
}
}
Output
unique letters: i,m,p,s
count of s: 4
How It Works
Drop repeats, then order.distinct() leaves the codes for m, i, s, p; sorted() puts them in numeric (alphabetical) order.
Back to strings.mapToObj(c -> String.valueOf((char) c)) turns each int into a one-letter String so Collectors.joining(",") can glue them.
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.