Our first program will demonstrate working with strings and characters in Java. Here’s the full source code:
To run the program, compile it and use java:
This Java program demonstrates key concepts about strings and characters:
In Java, strings are sequences of UTF-16 code units.
The length() method returns the number of UTF-16 code units in the string.
Individual characters can be accessed using charAt(), but this may not work correctly for characters outside the Basic Multilingual Plane.
To properly handle all Unicode characters, use methods like codePointCount(), codePoints(), and codePointAt().
The Character class provides utility methods for working with code points.
Java’s handling of strings is somewhat different from some other languages, as it uses UTF-16 encoding internally. This can sometimes lead to surprising results when dealing with characters outside the Basic Multilingual Plane, as they are represented by surrogate pairs.