char and boolean

The char Type

Example

A char holds one character in single quotes. Internally it is a number (its Unicode code), so you can do arithmetic on it.

public class Main {
    public static void main(String[] args) {
        char letter = 'J';
        char rupee = '\u20B9';
        char next = (char) (letter + 1);
        int code = letter;
        System.out.println(letter + " " + next + " " + code);
        System.out.println("Price: " + rupee + "499");
        System.out.println(Character.isDigit('7') + " " + Character.toLowerCase('Q'));
    }
}

Output

J K 74
Price: ₹499
true q

Note: Single quotes are for char ('A'), double quotes are for String ("A"). char c = "A"; does not compile.

The boolean Type

Example

A boolean is either true or false. It is the result of comparisons and controls if-statements and loops.

public class Main {
    public static void main(String[] args) {
        boolean isLoggedIn = true;
        boolean hasPremium = false;
        int age = 19;
        boolean canVote = age >= 18;
        System.out.println(isLoggedIn + " " + hasPremium + " " + canVote);
    }
}

Output

true false true

Note: Unlike C, Java does not treat 0/1 as booleans. if (1) is a compile error.