Quick revision
The short answer
Java's String.length() counts UTF-16 code units, not characters as a person sees them: a character outside the Basic Multilingual Plane, such as most emoji, takes two code units, a surrogate pair. Code points, counted with codePointCount or codePoints(), are Unicode characters; grapheme clusters, counted with BreakIterator or the \X regex, are what a user perceives as one character, which can be several code points; and bytes depend on the encoding, with UTF-8 using 1 to 4 bytes per code point. A 200-character name fails against a 255 limit because many databases measure that limit in bytes, or reject 4-byte characters entirely in a legacy 3-byte charset, and truncating with substring can split a surrogate pair and produce invalid text.
What to remember
In this answer, we separated four units: UTF-16 code units counted by length(), code points counted by codePointCount and codePoints(), grapheme clusters counted by BreakIterator or \X, and encoded bytes, where UTF-8 uses 1 to 4 bytes per code point. A family emoji measured 8, 5, 1 and 18 in those units. The 200-character insert failed because the database limit was in bytes or its charset could not hold 4-byte characters, and substring-based truncation can split a surrogate pair, so we truncate by code points, by encoded bytes or by graphemes and write the unit into every contract.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Java's String.length() counts UTF-16 code units, not characters as a person sees them: a character outside the Basic Multilingual Plane, such as most emoji, takes two code units, a surrogate pair. Code points, counted with codePointCount or codePoints(), are Unicode characters; grapheme clusters, counted with BreakIterator or the \X regex, are what a user perceives as one character, which can be several code points; and bytes depend on the encoding, with UTF-8 using 1 to 4 bytes per code point. A 200-character name fails against a 255 limit because many databases measure that limit in bytes, or reject 4-byte characters entirely in a legacy 3-byte charset, and truncating with substring can split a surrogate pair and produce invalid text.
Why this question is asked
Length limits are everywhere, in database columns, API fields, message sizes and UI inputs, and every one of them silently assumes a unit. When the units disagree, a value that passes validation in Java fails in the database, or a truncation corrupts an emoji into garbage. Interviewers ask this to see whether we can name the four units precisely and match each to its Java API. In this answer, we will define the four units, measure the same strings in each, explain why the insert failed, show safe truncation, and finish with how to choose the unit for a contract.
1A Quick Recap of the Four Units
A code point is a single Unicode character with a number, such as U+0041 for A or U+1F44D for the thumbs-up emoji. There are over a million possible code points. A Java char is a 16-bit UTF-16 code unit, which can hold code points up to U+FFFF, the Basic Multilingual Plane. Code points above that are stored as two chars, a high and a low surrogate, called a surrogate pair.
A grapheme cluster is what a reader sees as one character. It may be one code point, or several: e followed by a combining acute accent shows as one accented e, and a family emoji is several person emoji joined by zero-width joiners. Bytes are what an encoding produces: in UTF-8, ASCII takes 1 byte, most European and Middle Eastern letters 2, most Asian characters 3, and emoji and other supplementary characters 4.
2Measuring the Same Strings Four Ways
The clearest way to see the differences is to measure a few strings with each API.
| Unit | Java API | Count |
|---|---|---|
| UTF-16 code units | s.length() | 8 |
| Code points | s.codePointCount(0, s.length()), s.codePoints() | 5 |
| Grapheme clusters | BreakIterator, Pattern \X | 1 |
| UTF-8 bytes | s.getBytes(UTF_8).length | 18 |
Notice how far apart the numbers drift. The family emoji is one character to the user, five code points, eight Java chars and eighteen UTF-8 bytes. BreakIterator.getCharacterInstance() also finds grapheme boundaries; recent JDKs follow the current Unicode rules for emoji sequences, so check your version if exact emoji handling matters.
3Why the 200-Character Insert Failed
Many databases measure column limits in bytes, or store text in a way that adds limits of its own. In Oracle, VARCHAR2(255) uses byte semantics by default, so 200 characters of Polish, Greek or Japanese text, at 2 or 3 bytes each in UTF-8, exceed 255 bytes and the insert fails with a value-too-large error. In SQL Server, varchar(n) counts bytes. In MySQL, VARCHAR(255) counts characters, but a column in the legacy utf8 charset, which is utf8mb3 with at most 3 bytes per character, rejects any 4-byte character such as an emoji with an incorrect string value error. PostgreSQL's varchar(n) counts code points.
So the failure is usually one of two things: a byte-based limit with multi-byte characters, or a 3-byte charset that cannot store a 4-byte character at all. The Java side often hides both, because a validation of name.length() <= 255 counts UTF-16 units, which is neither bytes nor code points.
Notice that the check we should run before the insert depends on the database: bytes for Oracle byte semantics and SQL Server varchar, code points for PostgreSQL and MySQL, and for MySQL also whether the column is utf8mb4. The fix on the database side is often to declare character semantics, such as VARCHAR2(255 CHAR), or to use utf8mb4, and on the Java side to validate in the same unit.
4Truncating Without Breaking Text
When a value is too long, the tempting fix is substring(0, max). But substring works in code units, so it can cut between the two halves of a surrogate pair, leaving a lone surrogate, which is invalid Unicode. Encoders replace it with a question mark or reject it, so the stored text is corrupted.
Code-point and byte truncation keep the text valid, but they can still split a grapheme, for example dropping the combining accent after an e. For user-visible text, truncating at grapheme boundaries with BreakIterator is the most correct option.
5Choosing the Unit in a Contract
The lasting fix is to make the unit part of the contract. An API field described as name, max 255 characters is ambiguous; name, at most 255 Unicode code points, or at most 255 UTF-8 bytes, is not. Then the Java validation, the database column and the client all count the same thing. For user-facing limits, code points are a reasonable choice; for storage and wire limits, bytes are what the system actually enforces. Whichever we choose, tests should include accented letters, CJK text and emoji, because ASCII test data makes all four units equal and hides every bug in this answer.
6Follow-up questions
Is charAt safe for iterating over all characters?
Only if all text is in the Basic Multilingual Plane. For emoji and other supplementary characters, charAt returns surrogate halves. Iterating with s.codePoints() gives whole code points, and BreakIterator gives whole grapheme clusters.
Why does Java use UTF-16 code units for length at all?
Because when Java was designed, Unicode fit in 16 bits, so a char was a whole character. Unicode later grew beyond 65,536 code points, and Java kept char and length() for compatibility, adding code point methods in Java 5.
Does @Size in Bean Validation count characters or bytes?
@Size on a String uses length(), so it counts UTF-16 code units. For a byte or code point limit we need a custom constraint, or a check in code that matches the database's unit.
7Conclusion
In this answer, we separated four units: UTF-16 code units counted by length(), code points counted by codePointCount and codePoints(), grapheme clusters counted by BreakIterator or \X, and encoded bytes, where UTF-8 uses 1 to 4 bytes per code point. A family emoji measured 8, 5, 1 and 18 in those units. The 200-character insert failed because the database limit was in bytes or its charset could not hold 4-byte characters, and substring-based truncation can split a surrogate pair, so we truncate by code points, by encoded bytes or by graphemes and write the unit into every contract.
Open with
There are four different things people call length. Java's String.length counts UTF-16 code units, the chars. A code point is an actual Unicode character, and anything above U+FFFF, like most emoji, takes two chars, a surrogate pair, so codePointCount or codePoints gives the code point count.
Then, if they want more
A grapheme cluster is what a user sees as one character, which can be several code points, like e plus a combining accent, or a family emoji made of three people joined by zero-width joiners; BreakIterator or the backslash X regex counts those. And bytes depend on the encoding: UTF-8 uses one to four bytes per code point.
That family emoji is one grapheme, five code points, eight chars and eighteen UTF-8 bytes. So the 200-character name failing against 255 almost always means the limit is in bytes, like Oracle's default byte semantics or SQL Server varchar, and 200 Polish or Japanese characters are well over 255 bytes in UTF-8.
Or it is MySQL with the old three-byte utf8 charset, which rejects emoji outright. I would check the column definition, fix it with character semantics or utf8mb4, and validate in Java in the same unit, not with length.
I would also warn about truncation: substring can cut a surrogate pair in half and produce invalid text, so I truncate by code points with offsetByCodePoints, or by bytes with a CharsetEncoder, and by graphemes for display. And the contract should name the unit.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 5
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A column limit is 255 characters, but an insert of a 200-character name fails. Explain...": https://scrivio-interview-library.netlify.app/java/strings/d6701125f5/