Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The char data type in Java is a single, 16-bit unsigned primitive data type used to store a Unicode character. Strictly speaking, it represents a single UTF-16 code unit, allowing it to hold any character within the Unicode Basic Multilingual Plane (BMP).

Technical Specifications

  • Memory Size: 16 bits (2 bytes)
  • Minimum Value: \u0000 (integer value 0)
  • Maximum Value: \uffff (integer value 65,535)
  • Signedness: Unsigned (the only unsigned primitive type in Java)
  • Default Value: \u0000 (the null character)
  • Wrapper Class: java.lang.Character

Syntax and Initialization

A char literal is enclosed in single quotation marks. It can be initialized using a character literal, a Unicode escape sequence, or a direct integer value.
char letter = 'A';           // Standard character literal
char unicodeHex = '\u0041';  // Unicode escape sequence (Hexadecimal for 'A')
char numericVal = 65;        // Integer literal (Implicit narrowing conversion to 'A')

Integral Nature and Type Conversion

Because char is fundamentally an unsigned numeric type, it participates in standard arithmetic operations and bitwise manipulations. When a char is evaluated in an expression, it undergoes numeric promotion to an int.
char base = 'A';             // Integer value 65
int widened = base;          // Implicit widening conversion (widened = 65)

// Arithmetic promotion: 'base + 1' evaluates to an int (66).
// An explicit cast is required to assign it back to a char.
char nextChar = (char) (base + 1); // Results in 'B'

Escape Sequences

Java provides specific escape sequences to represent characters that cannot be typed directly or that have special syntactical meaning within the language.
char newline = '\n';         // Line feed (U+000A)
char carriageReturn = '\r';  // Carriage return (U+000D)
char tab = '\t';             // Horizontal tab (U+0009)
char singleQuote = '\'';     // Single quote (U+0027)
char doubleQuote = '\"';     // Double quote (U+0022)
char backslash = '\\';       // Backslash (U+005C)

Surrogate Pairs

Because a char is strictly 16 bits, it cannot natively represent Unicode supplementary characters (code points above U+FFFF). To represent these characters, Java uses a surrogate pair: two consecutive char values consisting of a high surrogate (\uD800 to \uDBFF) followed by a low surrogate (\uDC00 to \uDFFF).
// Representing the supplementary character U+10400 (Deseret Capital Letter Long I)
char highSurrogate = '\uD801';
char lowSurrogate = '\uDC00';

// In a String, this requires two char units:
String supplementaryStr = "\uD801\uDC00"; 
Master Java with Deep Grasping Methodology!Learn More