CKTIS / Java
char — a UTF-16 code unit
char is an unsigned primitive integral type that stores one 16-bit UTF-16 code unit. That unit does not necessarily represent a complete Unicode character; characters outside the BMP require a pair of char values.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
char letter = 'A';char is the keyword for an unsigned primitive integral type. Its range is 0–65535, or '\u0000' through '\uFFFF'. It stores one UTF-16 code unit, not an arbitrary complete Unicode character.
A char literal uses single quotes, such as 'A' or '\n'; "A" is instead a String literal. The empty literal '' is invalid. A char cannot hold null; Character is its object wrapper type. Fields and array elements of type char default to '\u0000', but a local variable must be assigned before it is read.
Characters outside the Basic Multilingual Plane (BMP), such as U+1F600, require two char units: a surrogate pair. A single char can also contain an isolated surrogate, so the type alone does not guarantee valid text. To represent the full range of code points, use int and the appropriate Character methods.
Arithmetic such as letter + 1 promotes the char value to int: the result is a number, not another char. The example prints 'A', its value 65, the addition result 66, and the code point 128512 reconstructed from a surrogate pair.
Examples
public class Main {
public static void main(String[] args) {
char letter = 'A';
char high = '\uD83D';
char low = '\uDE00';
int codePoint = Character.toCodePoint(high, low);
System.out.println(letter);
System.out.println((int) letter);
System.out.println(letter + 1);
System.out.println(codePoint);
}
}Result
A 65 66 128512
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.