Our Java program will demonstrate working with strings and characters. Here’s the full source code:
To run the program, compile and execute it using the javac and java commands:
This Java program demonstrates various aspects of working with strings and characters:
We define a string containing Thai characters.
We print the length of the string, which gives the number of UTF-16 code units.
We print the UTF-8 byte representation of the string.
We count the number of Unicode code points in the string.
We iterate over the string’s code points, printing each one along with its starting index.
We demonstrate an alternative method of iterating over code points.
We show how to compare code points with character literals.
Note that Java uses UTF-16 for its internal string representation, which is different from some other languages that use UTF-8. This can lead to some differences in how strings are handled, particularly with characters outside the Basic Multilingual Plane.