The char data type is used to store a single UTF-16 code unit in Java. It is commonly used for characters such as letters, digits, and symbols.
Unlike String, which represents a sequence of characters, char represents one character unit. For example, 'A', '7', and '@' can each be stored in a char.
What Is the char Data Type?
The char type is a 16-bit unsigned integer type that represents a UTF-16 code unit. Its values range from '\u0000' to '\uFFFF'.
char grade = 'A'; char digit = '5'; char symbol = '@';
A char literal must normally be written inside single quotes. Double quotes are used for String literals.
Key Point: A Java char occupies 16 bits and stores a UTF-16 code unit, not necessarily a complete Unicode character in every case.
Syntax of char
char variableName = 'character';
For example:
char firstLetter = 'B'; char answer = 'Y'; char specialSymbol = '#';
char Uses Single Quotes
One of the most common beginner mistakes is confusing character literals with string literals.
char letter = 'A'; String word = "A";
The first value is a single char, while the second is a String containing one character.
Remember: Use single quotes for a char and double quotes for a String.
Size and Range of char
| Property | Value |
|---|---|
| Data Type | char |
| Category | Character |
| Size | 16 bits |
| Minimum Value | '\u0000' or 0 |
| Maximum Value | '\uFFFF' or 65,535 |
| Default Value for a Field | '\u0000' |
| Signed | No |
char and Unicode
Java uses Unicode so that programs can represent characters from many writing systems rather than being restricted to basic English letters.
char english = 'A'; char symbol = '₹'; char bengali = 'ଓ';
This makes char useful when working with international text. However, Unicode has grown beyond the range of a single UTF-16 code unit, so some Unicode characters require a pair of char values known as a surrogate pair.
Character Escape Sequences
Java provides escape sequences for characters that are difficult or inconvenient to type directly.
char newline = '\n'; char tab = '\t'; char quote = '\''; char backslash = '\\';
| Escape | Meaning |
|---|---|
| \n | New line |
| \t | Tab |
| \' | Single quote |
| \\ | Backslash |
| \r | Carriage return |
| \b | Backspace |
char and Numeric Values
A char can participate in numeric operations because Java represents it using a numeric UTF-16 code unit value.
char letter = 'A'; int code = letter; System.out.println(code);
The variable code receives the numeric value associated with the character 'A'.
Adding to a char
Because char participates in numeric expressions, arithmetic operations can produce an int result.
char letter = 'A'; int nextCode = letter + 1; System.out.println(nextCode);
If you want the result stored back as a char, an explicit cast may be required.
char letter = 'A'; char nextLetter = (char) (letter + 1); System.out.println(nextLetter);
The output is 'B'.
char and int Conversion
A char can be automatically promoted to an int because its values fit within the integer range.
char letter = 'A'; int value = letter;
The reverse conversion from int to char requires an explicit cast.
int value = 65; char letter = (char) value; System.out.println(letter);
The output is 'A' because the numeric value corresponds to the UTF-16 code unit for that character.
char in Conditions
Characters are frequently used in conditions when validating input or checking specific symbols.
char grade = 'A';
if (grade == 'A') {
System.out.println("Excellent");
}
This pattern is common in grading systems, menu selections, command processing, and character validation.
char in Loops
A char can also be changed inside a loop because it has an underlying numeric value.
for (char letter = 'A'; letter <= 'E'; letter++) {
System.out.println(letter);
}
The loop prints the characters from A through E.
char vs String
| Feature | char | String |
|---|---|---|
| Stores | One UTF-16 code unit | A sequence of characters |
| Literal Syntax | Single quotes | Double quotes |
| Example | 'A' | "A" |
| Primitive Type | Yes | No |
| Size | 16 bits | Variable length |
Common Beginner Mistakes
- Using double quotes instead of single quotes for a char.
- Assuming char can store an entire text or word.
- Forgetting that char is an unsigned 16-bit type.
- Assuming every Unicode character always fits into one char.
- Expecting arithmetic involving char to automatically produce another char.
Best Practices
- Use char when you need a single UTF-16 code unit.
- Use String when working with text or sequences of characters.
- Use character escape sequences when they make special characters clearer.
- Be aware of UTF-16 surrogate pairs when working with Unicode code points outside the Basic Multilingual Plane.
- Use appropriate character-handling APIs when processing international text rather than assuming one char always equals one user-visible character.
Interview Insights
Question: What is the size of char in Java?
Answer: char is 16 bits and represents a UTF-16 code unit.
Question: Is char signed or unsigned in Java?
Answer: char is an unsigned 16-bit type with values from 0 to 65,535.
Question: What is the difference between 'A' and "A"?
Answer: 'A' is a char literal, while "A" is a String containing one character.
char Data Type: Quick Revision
| Property | Details |
|---|---|
| Type | char |
| Category | Character |
| Size | 16 bits |
| Minimum | '\u0000' or 0 |
| Maximum | '\uFFFF' or 65,535 |
| Default Field Value | '\u0000' |
| Signed | No |
| Literal Syntax | 'A' |
The char data type connects Java's primitive type system with Unicode text. The most important idea to remember is that Java char represents a 16-bit UTF-16 code unit, not simply an unrestricted "character." Once this distinction is clear, character literals, Unicode values, escape sequences, and character arithmetic become much easier to understand.
