Characters may look like ordinary text, but Java treats a char as a numeric primitive type with a 16-bit unsigned representation. This makes character conversion especially interesting because a character can participate in numeric conversion, arithmetic, comparisons, and explicit casting.
Understanding character conversion helps explain why 'A' + 1 produces a number instead of another character, why converting an integer to a character can produce a completely different symbol, and why a String cannot simply be cast to char.
Important: A Java char represents a UTF-16 code unit, not an arbitrary text string. It can hold a single 16-bit value from '\u0000' through '\uFFFF'.
Why Does Character Conversion Exist?
Computers ultimately work with numeric representations. Java therefore assigns every char a numeric value. This allows characters to be compared, incremented, converted to numbers, and processed by algorithms.
For example, the character 'A' has the numeric value 65, while 'B' has the value 66. This relationship makes character-based programming surprisingly powerful.
char to int
Converting a char to an int is a widening primitive conversion and normally happens automatically.
char letter = 'A'; int code = letter; System.out.println(code);
The output is 65. Java promotes the character to its numeric UTF-16 code-unit value.
Remember: Assigning a char to an int gives you its numeric code-unit value, not a text representation of the character.
char to long
A character can also be widened to long.
char symbol = 'Z'; long value = symbol; System.out.println(value);
The character's numeric value is automatically converted to long.
int to char
The reverse operation is a narrowing conversion. An integer can be explicitly converted to a char.
int code = 65; char letter = (char) code; System.out.println(letter);
The output is 'A' because the numeric value 65 corresponds to the UTF-16 code unit for that character.
This technique is useful when an algorithm works with numeric character values and you eventually need to turn the value back into a character.
Character Arithmetic
Because char participates in numeric promotion, arithmetic involving characters normally produces an int.
char letter = 'A'; int next = letter + 1; System.out.println(next);
The result is 66. Java promotes letter to int before performing the addition.
If you want the result as another character, you need an explicit cast.
char letter = 'A'; char next = (char) (letter + 1); System.out.println(next);
The output is 'B'.
Converting Lowercase to Uppercase Using Character Arithmetic
Character arithmetic can demonstrate how character ranges can be manipulated, although production code should generally use the standard character-processing APIs when handling real-world text.
char lower = 'a'; char upper = (char) (lower - 32); System.out.println(upper);
For basic Latin letters, this produces 'A'. However, manually assuming a fixed numeric offset is not a good general solution for international text.
Best practice: For production text processing, prefer Java's character and string APIs instead of assuming that every alphabetic character follows the same numeric relationship.
Character Comparison
Because characters have numeric values, Java can compare them using relational operators.
char first = 'A'; char second = 'B'; System.out.println(first < second);
The result is true because the numeric value of 'A' is smaller than the numeric value of 'B'.
char and byte Are Not the Same
Both byte and char are small primitive types, but their ranges and purposes are different.
| Type | Size | Signed? | Typical Purpose |
|---|---|---|---|
| byte | 8-bit | Yes | Small integer values and binary data |
| char | 16-bit | No | UTF-16 code units |
A byte ranges from -128 to 127, while a char ranges from 0 to 65535.
byte to char
A byte cannot be directly assigned to a char because their numeric conversion rules do not allow a direct implicit conversion.
byte value = 65; char letter = (char) value; System.out.println(letter);
The explicit cast first converts the numeric value into a char. Since 65 is within the valid char range, the result is 'A'.
char to byte
The reverse conversion requires an explicit cast because a char can represent values far beyond the range of a byte.
char letter = 'A'; byte value = (byte) letter; System.out.println(value);
This produces 65. However, if the character's numeric value exceeds the byte range, information can be lost.
Character Literals and Integer Constants
Java allows an integer constant that fits within the char range to be assigned directly to a character variable.
char letter = 65;
System.out.println(letter);
The output is A.
But this does not mean every integer variable can be assigned to a char without a cast.
int code = 65; // Invalid: // char letter = code;
The variable is an int, so an explicit conversion is required.
Character Conversion and Unicode
Java uses UTF-16 for its char representation. This means a char represents one UTF-16 code unit, not necessarily one complete Unicode character.
This distinction matters for characters whose Unicode code points require surrogate pairs. A single char cannot represent every Unicode code point by itself.
Important insight: In everyday Java programming, char is often described as a character type. Technically, it is a UTF-16 code-unit type, so Unicode text can sometimes require more than one char.
char and String Are Different
A char represents one UTF-16 code unit, while a String represents a sequence of characters. Therefore, they are fundamentally different types.
char letter = 'A'; String text = "A";
Both appear to contain the letter A, but the first is a primitive char and the second is a String object containing a sequence of UTF-16 code units.
You cannot convert between them using an ordinary primitive cast.
Common Mistakes
- Assuming char and String are interchangeable.
- Forgetting that arithmetic involving char normally produces an int.
- Assuming every Unicode character fits into one Java char.
- Converting an integer to char without checking whether the value represents the intended code unit.
- Using manual ASCII-style arithmetic as a universal solution for international text.
Best Practices
- Use char when you specifically need a UTF-16 code unit.
- Use String for normal text and sequences of characters.
- Use explicit casts when converting numeric values to char.
- Use Java's character and Unicode APIs for robust international text processing.
- Remember that char participates in numeric promotion during arithmetic.
Interview Insight
A common interview question is: Why does char + char produce int in Java? The answer is that operands of type char undergo numeric promotion to int before arithmetic is performed. Therefore, the result of the addition is an int, not a char.
| Conversion | Result | Key Point |
|---|---|---|
| char → int | Automatic | Character becomes its UTF-16 code-unit value. |
| int → char | Explicit cast normally required | Numeric value becomes a UTF-16 code unit. |
| char + char | int | Characters are promoted before arithmetic. |
| byte → char | Explicit cast | The types have different ranges and conversion rules. |
| char → byte | Explicit cast | Values outside byte range can lose information. |
| char ↔ String | Not a primitive cast | They represent fundamentally different kinds of values. |
Character conversion becomes much easier once you stop thinking of char as simply "a letter" and instead understand it as a UTF-16 code unit with a numeric value. That single idea explains automatic conversion to numeric types, character arithmetic, explicit conversion back from integers, and the important difference between char and String. With that foundation, Java's character handling becomes predictable rather than mysterious.
