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:
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:
char c = 'a';puts the characteraintoc, and inside it is its number, 97.println(c)prints a character:printlnsees that it has achar.(int) ccasts the character tointand shows the number,97.c + 1is already arithmetic: the result has typeint, and98is printed.(char) (c + 1)casts the number98back tochar, andbis 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
intor with anotherchar, the character turns into anint, and the result is aninttoo; - 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:
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); // B1first == 'B'givestrue.charis a primitive type, so==compares the values themselves, not references as with strings.first + 1adds the code ofB, 66, to one and prints67.- In
"" + first + 1there is an empty string on the left, so both+glue, andB1is 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:
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); // 3The 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 +:
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
charholds a character code, a 16-bit number;(int) cshows the code, and(char)turns a code into a character. - Arithmetic on
charandintgives anint:'a' + 1is98, and twocharvalues joined by+are added as numbers. - To glue characters, you need a string on the left:
"" + c1 + c2. charAtreturns achar, and the classCharacterchecks characters:isDigit,isLetter.