Skip to content
Theory · Strings · lesson 9 of 11

The char type

The charAt method returns a single character, not a string: a value of type char. In this lesson we'll see what lies inside such a value and why characters are sometimes added up like numbers.

Back to the card. Each of its cells shows a letter, but a computer stores numbers, not letters. Every character has a number in the shared character table, Unicode: a is 97, b is 98, A is 65. A cell of the card really holds this number, and println turns it back into a letter when it prints:

JAVA
public class Main {
    public static void main(String[] args) {
        char c = 'a';
        System.out.println(c);              // a
        System.out.println((int) c);        // 97
        System.out.println(c + 1);          // 98
        System.out.println((char) (c + 1)); // b
    }
}

Let's go through the program line by line:

  1. char c = 'a'; puts the character a into c, and inside it is its number, 97.
  2. println(c) prints a character: println sees that it has a char.
  3. (int) c casts the character to int and shows the number, 97.
  4. c + 1 is already arithmetic: the result has type int, and 98 is printed.
  5. (char) (c + 1) casts the number 98 back to char, and b is printed.

A char is a number

A character code is a character's number in the Unicode table. A char value is an unsigned 16-bit number from 0 to 65535, and what it holds is exactly the code. That is why arithmetic works on char:

  • in an expression with an int or with another char, the character turns into an int, and the result is an int too;
  • to get a character back, you need the cast (char);
  • the codes of Latin letters and digits run in order: 'a' is 97, 'b' is 98, 'A' is 65, '0' is 48.

char and String in one expression

Remember the joining rule: if a string stands on one side of +, + glues. If there is no string, + adds numbers, and a char is a number:

JAVA
String name = "Bob";
char first = name.charAt(0);
System.out.println(first == 'B');   // true
System.out.println(first + 1);      // 67
System.out.println("" + first + 1); // B1
  1. first == 'B' gives true. char is a primitive type, so == compares the values themselves, not references as with strings.
  2. first + 1 adds the code of B, 66, to one and prints 67.
  3. In "" + first + 1 there is an empty string on the left, so both + glue, and B1 is printed.

Interviewers like to ask what the sum of two characters in println prints. The same rule gives the answer: no string means the codes are added.

The card picture is off here. It looks as if every character takes exactly one cell. But 16 bits are not enough for everything: many emoji take two char cells each in a string, and "😀".length() gives 2.

Checking characters

The class Character has ready-made methods for checking characters:

  • Character.isDigit(c) tells whether it is a digit;
  • Character.isLetter(c) tells whether it is a letter;
  • Character.toUpperCase(c) gives the same letter as a capital.

These are static methods: you call them through the class name, and they need no import. Here is a count of the digits in a string:

JAVA
String code = "A7-42b";
int digits = 0;
for (int i = 0; i < code.length(); i++) {
    if (Character.isDigit(code.charAt(i))) {
        digits++;
    }
}
System.out.println(digits); // 3

The loop goes through the indexes from 0 to length() - 1. charAt(i) takes the next character, and Character.isDigit checks whether it is a digit. The string holds three digits: 7, 4 and 2.

Common mistake

Initials are built from two charAt calls with +:

JAVA
String first = "Anna";
String last = "Lee";
System.out.println(first.charAt(0) + last.charAt(0)); // ✗

The goal was to print AL, but the program prints 141. charAt returns a char, there is no string in the expression, and + adds the codes 65 and 76. Put an empty string on the left: "" + first.charAt(0) + last.charAt(0) prints AL.

Key points

  • A char holds a character code, a 16-bit number; (int) c shows the code, and (char) turns a code into a character.
  • Arithmetic on char and int gives an int: 'a' + 1 is 98, and two char values joined by + are added as numbers.
  • To glue characters, you need a string on the left: "" + c1 + c2.
  • charAt returns a char, and the class Character checks characters: isDigit, isLetter.