What is the difference between `String.chars()`, `String.codePoints()`, and how do they differ for supplementary characters?

Java Professional Hard

Java Professional — Hard

What is the difference between `String.chars()`, `String.codePoints()`, and how do they differ for supplementary characters?

Key points

  • `chars()` returns UTF-16 code units, splitting supplementary characters
  • `codePoints()` returns Unicode code points, handling supplementary characters correctly
  • Supplementary characters are not always split into surrogates
  • `codePoints()` is not deprecated in favor of `chars()`
  • `chars()` does not handle supplementary characters correctly

Ready to go further?

Related questions