How is char different from the other primitive types in Java?
answer
- char = 16-bit UNSIGNED, range 0..65535
- Only unsigned primitive; others are signed two's-complement
- Holds a UTF-16 code unit; numeric, supports arithmetic
- 'A' single quotes = char; "A" double quotes = String
- Above U+FFFF needs a surrogate pair (two chars)
basics
~10 schar is a 16-bit unsigned number holding one UTF-16 character code (0 to 65535). It is the only unsigned primitive, and you can do arithmetic on it because it is numeric under the hood.
solid answer
~40 schar is a 16-bit primitive that holds a single UTF-16 code unit, with values from 0 to 65,535. It is unique among Java primitives for being unsigned — every integer primitive (byte, short, int, long) is signed two's-complement, but char has no negative values. Although it represents a character, char is fundamentally a numeric type: you can do arithmetic ('A' + 1 == 66), compare with <, and it auto-promotes to int in expressions. A char literal uses single quotes ('A'), distinct from a String's double quotes. One important limitation: char holds 16 bits, but some Unicode characters (code points above U+FFFF, like many emoji) need two chars — a surrogate pair — so a single char cannot always represent a full Unicode character.
code
java · 10 lineschar a = 'A';
int code = a; // 65 (widened to int)
char b = (char)(a + 1); // 'B'
System.out.println(b); // B
System.out.println('z' - 'a'); // 25
// Emoji needs two chars (surrogate pair):
String smile = "😀"; // grinning face
System.out.println(smile.length()); // 2 (code units)
System.out.println(smile.codePointCount(0, smile.length())); // 1go deeper
Knows char holds a single character, uses single quotes, and is 16 bits.
Knows char is unsigned 0-65535, is numeric (supports arithmetic and widening to int), and distinguishes it from String.
Explains UTF-16 code units, surrogate pairs for supplementary characters, and the code-unit vs code-point distinction in String APIs.
Reasons about text-handling correctness at scale (normalization, code-point iteration, i18n) and sets conventions to avoid surrogate-related bugs across the codebase.
## What char is `char` is a **16-bit** primitive type. Its value range is **0 to 65,535** — and that is the first thing that sets it apart: **char is the only unsigned primitive in Java**. Every other integer primitive (`byte`, `short`, `int`, `long`) is *signed* two's-complement and can hold negatives; char cannot go below zero. ## char is really a number Though we think of it as 'a character', a `char` literally stores a **number** — specifically a **UTF-16 code unit**, which is Java's chosen encoding for text. Each number maps to a character via the Unicode standard: 65 is `'A'`, 97 is `'a'`, 48 is `'0'`. Because it is numeric, char participates in arithmetic and comparisons: ```java char c = 'A'; // the number 65 int x = c + 1; // 66 char next = (char)(c + 1); // 'B' boolean b = 'a' < 'b'; // true ``` In any arithmetic expression a `char` is **automatically widened to int** (this is *numeric promotion*), which is why `c + 1` has type int and needs a cast back to char. ## Literals: single vs double quotes A char literal uses **single quotes** and holds exactly one code unit: `'A'`, `'\n'`, `'A'` (Unicode escape). Double quotes make a **String** object, which is a reference type, not a primitive: `"A"`. Mixing them up is a common beginner error. ## The 16-bit limitation: surrogate pairs UTF-16 can encode characters in the **Basic Multilingual Plane** (code points U+0000 to U+FFFF) in a single 16-bit unit. But Unicode has more than 65,536 characters. Characters above U+FFFF — many emoji, some historic scripts — are **supplementary characters** and require **two** chars together, called a **surrogate pair**. So a single `char` cannot represent every Unicode character; for full code-point handling you work with `int` code points (`String.codePointAt`, `Character.toChars`). `String.length()` counts code *units*, not characters, which is why an emoji can make `length()` return 2. ## Default value The default value of an uninitialized `char` field is `''` (the null character, numeric 0), not a space and not null — char can never be null because it is a primitive. ## Summary of differences - **Unsigned** (unique among primitives). - Holds a **UTF-16 code unit**, semantically a character but numerically an integer 0-65535. - Uses **single-quote** literals; double quotes are Strings. - **16 bits is not enough for all Unicode** — supplementary characters need surrogate pairs.
- What is the default value of an uninitialized char field?'', the null character (numeric value 0). It is never null, since char is a primitive.
- Why can a single emoji make String.length() return 2?Emoji above U+FFFF are supplementary characters encoded as a surrogate pair — two 16-bit chars — and length() counts code units, not characters.
saying these in an interview costs you the question
- Saying char is signed or can be negative
- Thinking one char always equals one Unicode character (emoji break this)
- Using double quotes for a char literal
- Believing char can be null (primitives are never null; default is '')