If you need to know how to get the length of a string in Java, call the string’s length() method: string.length(). This simple operation returns an int, but its exact meaning matters because Java counts UTF-16 code units, not necessarily the number of letters or symbols a person recognizes as characters. Understanding that distinction helps prevent surprises with emojis, accented letters, and other Unicode text.
Introduction: What Does String Length Mean?
A string in Java is an immutable sequence of characters stored using UTF-16 encoding. When you call length(), Java returns the number of 16-bit char values contained in the string.
For ordinary English text, this usually matches the number of visible characters:
String name = "Java";
int length = name.length();
System.out.println(length); // 4
The string "Java" contains four char values: J, a, v, and a. That said, Unicode can represent some symbols using more than one Java char. This leads to length() can return a number greater than the number of visible characters in the string.
Easier said than done, but still worth knowing.
How to Get the Length of a String in Java
The standard approach is to use the built-in length() method on a String object:
public class Main {
public static void main(String[] args) {
String message = "Hello, Java!";
int result = message.length();
System.out.println(result);
}
}
Output:
13
The result is 13 because the string contains 12 letters and spaces plus one exclamation mark.
You can also perform the operation directly when printing or storing the value:
String text = "Programming";
System.out.println(text.length()); // 11
The method is lowercase because it follows Java’s convention for instance methods. This is different from an array’s length property:
String text = "Hello";
char[] characters = text.toCharArray();
System.length()); // String method: 5
System.In practice, out. Because of that, println(text. out.println(characters.
For most string operations, `text.length()` is the clearest and most efficient choice.
## Important Distinction: `length()` Counts UTF-16 Code Units
Java’s `char` type represents a 16-bit UTF-16 code unit. Many common characters occupy one code unit, but characters outside the Basic Multilingual Plane can require a pair called a **surrogate pair**.
Here's one way to look at it: the smiling face emoji `😀` has the Unicode code point U+1F600. In Java, it is stored using two `char` values:
```java
String emoji = "😀";
System.out.println(emoji.length()); // 2
Although people see one emoji, Java reports a length of 2.
The same issue can affect accented characters. A name such as Zoë may be stored in two valid Unicode forms:
- Precomposed form: one code point for
ë - Decomposed form: the letter
efollowed by a combining diaeresis
The decomposed version can produce a different result from length(), even though both versions appear as Zoë on screen Which is the point..
That's why, length() answers the question, “How many UTF-16 code units are in this string?” It does not always answer, “How many visible characters or Unicode code points are there?”
Counting Unicode Code Points Instead
If your application needs the number of Unicode code points rather than Java’s internal UTF-16 units, use codePointCount():
String emoji = "😀";
int codePoints = emoji.codePointCount(0, emoji.length());
System.out.println(codePoints); // 1
The method accepts a starting index and an ending index. The ending index is exclusive, just as it is for many other Java range-based methods.
You can also calculate the code-point count of an entire string:
String text = "A😀B";
int count = text.codePointCount(0, text.length());
System.out.println(count); // 3
This