How to compare chars in Java?

To compare chars in Java, use the == operator like so: char1 == char2.

In the example below, the first print statement will output false and the second one true:

char char1 = 'a';
char char2 = 'b';
char char3 = 'a';

// false
System.out.println(char1 == char2);

// true
System.out.println(char1 == char3);