Character information in Java SE 8 is based on the Unicode Standard, version 6.2.0 [Unicode 2012]. However, Java programs must often work with string data represented in various character sets. Java 7 introduced the StandardCharsets
Class that specifies character sets that are guaranteed to be available on every implementation of the Java platform including ISO Latin Alphabet No. 1, Seven-bit ASCII, UTF 8, and UTF 16.
Legacy software frequently assumes that every character in a string occupies 8 bits (a Java byte
). The Java language assumes that every character in a string occupies 16 bits (a Java char
). Unfortunately, neither the Java byte
nor Java char
data types can represent all possible Unicode characters. Many strings are stored or communicated using encodings such as UTF-8 that support characters with varying sizes.
...
The trailing byte ranges overlap the range of both the single-byte and lead-byte characters. When a multibyte character is separated across a buffer boundary, it can be interpreted differently than if it were not separated across the buffer boundary; this difference arises because of the ambiguity of its composing bytes [Phillips 2005].
Supplementary Characters
...
The char
data type
...
is based on the original Unicode specification, which defined characters as fixed-width 16-bit entities. The Unicode
...
Standard has since been changed to allow for characters whose representation requires more than 16 bits. The range of legal code
...
points is now
...
U+0000 to U+10FFFF, known as Unicode scalar value.Characters whose code points are greater than U+FFFF are called supplementary characters. Such characters are generally rare, but some are used, for example, as part of Chinese and Japanese personal names. To support supplementary characters without changing the char
primitive data type and causing incompatibility with previous Java programs, supplementary characters are defined by a pair of code point values that are called surrogates. According to the Java API [API 2014] class Character
documentation (Unicode Character Representations):
The Java 2 platform uses the UTF-16 representation in
char
arrays and in theString
andStringBuffer
classes. In this representation, supplementary characters are represented as a pair ofchar
values, the first from the high-surrogates range, (\uD800-\uDBFF), the second from the low-surrogates range (\uDC00-\uDFFF).An
The methods that only accept aint
value represents all Unicode code points, including supplementary code points. The lower (least significant) 21 bits ofint
are used to represent Unicode code points, and the upper (most significant) 11 bits must be zero. Unless otherwise specified, the behavior with respect to supplementary characters and surrogate char values is as follows:char
value cannot support supplementary characters. They treatchar
values from the surrogate ranges as undefined characters. For example, Character.isLetter('\uD840')
returnsfalse
, even though this specific value if followed by any low-surrogate value in a string would represent a letter.The methods that accept anint
value support all Unicode characters, including supplementary characters.
For example,Character.isLetter(0x2F81A)
returnstrue
because the code point value represents a letter (a CJK ideograph).
Noncompliant Code Example (Read)
...