Course contentsShow
← Java
Lesson 51 of 2,8702. Variables and Primitive TypesFree lesson

char as a Numeric Type: Arithmetic with Characters

Discover that char is an unsigned 16-bit integer type and can participate in arithmetic operations.

char as a Numeric Type: Arithmetic with Characters

You'll learn that char isn't just for letters—it's actually a number type that you can use in math operations.

The Surprising Truth About char

In the previous lesson, you learned that char holds single characters using Unicode. Here's the twist: under the hood, char is actually an unsigned 16-bit integer. Each character is stored as a number from 0 to 65,535 representing its Unicode code point.

Because char is fundamentally numeric, Java lets you perform arithmetic operations on characters just like you would with int or long.

How It Works

When you use a char in arithmetic, Java automatically converts it to its numeric Unicode value:

char letter = 'A';  // 'A' has Unicode value 65
char next = letter + 1;  // 65 + 1 = 66 = 'B'
System.out.println(next);  // prints: B

char digit = '5';  // '5' has Unicode value 53
int numericValue = digit - '0';  // 53 - 48 = 5
System.out.println(numericValue);  // prints: 5

Think of it like this: every character is wearing a numbered jersey. When you do math with characters, you're really doing math with those jersey numbers.

Why This Matters

This numeric nature allows clever tricks, like converting between uppercase and lowercase (they're exactly 32 positions apart in Unicode), or finding the actual numeric value of a digit character by subtracting '0'.

Key Takeaway: char is an unsigned 16-bit integer type that represents characters through Unicode numbers, allowing you to perform arithmetic operations that manipulate characters mathematically.