The char Primitive
Characters and Unicode.
The char Primitive is a free Java Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a char?
A char is a Java primitive that holds a single character, like a letter, digit, or symbol.
You write a char value between single quotes: 'A', '7', or '?'. Double quotes make a String instead, so "A" is not a char.
char grade = 'A';
char symbol = '#';Printing a char
You can store a char in a variable and print it just like any other value.
Here a complete program declares a char and prints it. Notice the single quotes around the letter.
public class Main {
public static void main(String[] args) {
char first = 'J';
System.out.println(first);
}
}char Is Really a Number
Under the hood, every char has a numeric code from the Unicode table. The letter 'A' is code 65, and 'a' is code 97.
If you assign a char to an int, Java gives you that code number.
public class Main {
public static void main(String[] args) {
char c = 'A';
int code = c;
System.out.println(code);
}
}Doing Math With char
Because a char is a number, you can do arithmetic with it. Adding 1 to 'A' moves to the next code, which is 'B'.
To see it as a letter again, cast the result back to char.
public class Main {
public static void main(String[] args) {
char a = 'A';
char next = (char) (a + 1);
System.out.println(next);
}
}Digit Characters vs Numbers
Be careful: the char '5' is not the number 5. The character '5' has Unicode code 53.
To convert a digit char to its real number value, subtract '0' from it. This works because digit codes are in order.
public class Main {
public static void main(String[] args) {
char d = '5';
int value = d - '0';
System.out.println(value);
}
}Special Escape Characters
Some characters are written with a backslash escape. '\n' is a newline, '\t' is a tab, and '\'' is a single quote.
Each escape is still a single char even though it uses two symbols in your code.
char tab = '\t';
char quote = '\'';
char newline = '\n';Comparing Characters
You can compare chars with ==, <, and > because they compare the underlying numbers.
Since 'a' is 97 and 'b' is 98, the expression 'a' < 'b' is true.
public class Main {
public static void main(String[] args) {
char x = 'a';
char y = 'b';
System.out.println(x < y);
}
}char in a switch
A char works nicely inside a switch statement to choose between cases.
Each case label is a char in single quotes. This is handy for menu choices or grading.
public class Main {
public static void main(String[] args) {
char grade = 'B';
switch (grade) {
case 'A': System.out.println("Great"); break;
case 'B': System.out.println("Good"); break;
default: System.out.println("Keep going");
}
}
}Default char Value
If a char field is not given a value, its default is the null character with code 0, written '\u0000'.
Local variables in methods have no default, so you must assign a char before using it or the code will not compile.
char letter;
letter = 'Z';
System.out.println(letter);Unicode Escapes
You can write any char by its Unicode code using '\u' followed by four hex digits.
For example '\u0041' is the letter 'A'. This lets you include characters that are hard to type directly.
public class Main {
public static void main(String[] args) {
char a = '\u0041';
System.out.println(a);
}
}char Range Limits
A char is an unsigned 16-bit value, so it can hold codes from 0 to 65535. It cannot be negative.
This is enough room for letters, digits, and characters from many world languages stored in Unicode.
public class Main {
public static void main(String[] args) {
char max = (char) 65535;
int code = max;
System.out.println(code);
}
}Quick Check
Test your understanding of the char primitive.
Recap
A char holds one character in single quotes and is backed by a Unicode number.
You can do math on chars, compare them, and convert digit chars to numbers by subtracting '0'. Casting back to char turns a code into a visible character.
Frequently asked questions
Is the “The char Primitive” lesson free?
Yes — the full text of “The char Primitive” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “The char Primitive”?
Characters and Unicode. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The char Primitive” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.