How are char values treated in arithmetic expressions, and what does ''a' + 1' evaluate to?
answer
- char = unsigned 16-bit number (code point)
- char promotes to int in arithmetic
- 'a' == 97, 'A' == 65, '0' == 48
- 'a' + 1 = int 98, not 'b'
- String + char = concatenation, not numeric
basics
~10 sIn arithmetic, a char is treated as its numeric Unicode code value and promoted to int. 'a' has code 97, so 'a' + 1 evaluates to the int 98, not the character 'b'.
solid answer
~40 sA char in Java is a 16-bit unsigned integral type holding a Unicode code unit. In any arithmetic or comparison expression a char undergoes numeric promotion: it becomes an int equal to its code value. So 'a' (code 97) + 1 yields the int 98 — an int, not a char. If you want the character 'b', you must cast back: (char)('a' + 1). This is why concatenation versus arithmetic matters: "x=" + 'a' produces the string "x=a" (string concatenation), but 'a' + 1 produces 98 (numeric). Comparisons like 'a' < 'b' also work because both promote to int (97 < 98 is true). The takeaway: char participates fully in integer arithmetic via its code point, and the result is at least int.
code
java · 7 linesint n = 'a' + 1; // 98 (int)
char next = (char)('a' + 1); // 'b'
System.out.println('a' + 'b'); // 195 (numeric)
System.out.println("" + 'a' + 1); // "a1" (concatenation)
int distance = 'g' - 'a'; // 6, handy for alphabet indexinggo deeper
Knows a char is really a number and that 'a' + 1 is 98, needing a cast to become a character.
Explains char promotion to int and the String-concatenation overload of +, including left-to-right evaluation order.
Discusses char as an unsigned 16-bit code unit, surrogate pairs/code points beyond the BMP, and how promotion interacts with formatting.
Can reason about Unicode code-unit vs code-point pitfalls, API design choices (char vs int code points), and i18n implications of treating chars as numbers.
## char is a number A Java `char` is a 16-bit **unsigned** integral type. Each `char` literal like `'a'` stores a number: its Unicode code unit. `'a'` is 97, `'A'` is 65, `'0'` is 48, `' '` is 32. This means `char` is genuinely one of Java's integral numeric types — you can do arithmetic on it. ## Promotion in expressions When a `char` appears as an operand of an arithmetic operator (`+ - * / %`), a comparison (`< > <= >=`), or a bitwise operator, **numeric promotion** kicks in. Because `char` is smaller than `int` (well, same bit width as `short` but the rule still applies), it is promoted to `int` using its code value. So evaluating `'a' + 1`: 1. `'a'` is promoted to the `int` 97. 2. `1` is already an `int`. 3. `97 + 1` = `98`, of type `int`. The result is the **int** `98`, **not** the char `'b'`. If you write `System.out.println('a' + 1)` you see `98`. To get the character you must narrow back: `(char)('a' + 1)` gives `'b'`. ## The concatenation trap The `+` operator is overloaded. If **either** operand is a `String`, `+` means string concatenation and the char is converted to its **character form**, not its number: ```java System.out.println('a' + 1); // 98 (numeric: both numeric) System.out.println("" + 'a' + 1); // a1 (concatenation: char -> "a", then "a"+"1") System.out.println('a' + 1 + ""); // 98 (left-to-right: 'a'+1 = 98 numeric, then 98 + "" = "98") ``` Evaluation is left-to-right, so operator grouping decides whether you get numeric promotion or concatenation. This is a classic interview gotcha. ## Comparisons and loops Because chars promote to int, you can compare and iterate: ```java for (char c = 'a'; c <= 'z'; c++) { // works: c promoted to int for <= and ++ System.out.print(c); } ``` Here `c <= 'z'` compares 97..122 against 122; `c++` increments the code value (and the compound increment includes the implicit narrowing cast back to char). ## Deriving any answer Replace the char with its code value, apply the int-floor promotion rule, and you have the type and value. `'a' + 'b'` = 97 + 98 = the int 195. `'b' - 'a'` = 1 (an int, useful for indexing into alphabets).
- What does System.out.println('a' + 'b') print and why?195. Both chars are promoted to int (97 and 98) and added numerically, giving the int 195. No String is involved, so + is arithmetic, not concatenation.
- How would you get the character three letters after 'a'?Cast the int result back to char: (char)('a' + 3), which is 'd'. Without the cast, 'a' + 3 is the int 100.
saying these in an interview costs you the question
- Claiming 'a' + 1 produces the char 'b'
- Thinking char is not a numeric type
- Assuming + always concatenates when a char is involved
- Forgetting the cast needed to get a char back from char arithmetic