In Java, char is essentially an unsigned short. To iterate over a string containing unicode characters outside the range supported by char (first 65536), you should use the following pattern, which stores each code as an int.
for (int i = 0; i < str.length();) { int ch = str.codePointAt(i);
Java was designed with first-class support for the first 65,536 characters, which at that time was an improvement over C / C ++, which had first-class support for the first 128 or 256 characters only. Unfortunately, this means that the above pattern is needed in Java to support out-of-range characters, which are becoming more common.
source share