AI Hints
Home›Java Tutorial›char and boolean, Type Casting, Practice: A Simple Calculator
Java · Lesson 16 of 120

char and boolean

Letters and yes or no.

⏱ 2 min read▶ Video at 20:42🎓 Free
Watch this lesson in the Java courseWatch on YouTube ↗Full Java course

A char holds exactly one character, like a letter, or a digit. A boolean holds one of two values, true, or false.

  • char: one character
  • boolean: true or false
  • Simple, but useful

char and boolean

A char uses single quotes. Joined into text, chars print as letters. But adding two chars on their own, gives a number! That's because each char is stored as a number underneath. A boolean is true, or false. And comparisons, like five greater than three, produce a boolean. Run it. Notice the strange one hundred and seventy one, that's J plus a, as numbers.

Main.javajava
public class Main {
    public static void main(String[] args) {
        char first = 'J';
        char next = 'a';
        System.out.println(first);
        System.out.println("Letters: " + first + next);
        System.out.println(first + next);

        boolean raining = true;
        boolean sunny = false;
        System.out.println("Raining? " + raining);
        System.out.println("Sunny? " + sunny);
        System.out.println("Is 5 > 3? " + (5 > 3));
    }
}
Output
J
Letters: Ja
171
Raining? true
Sunny? false
Is 5 > 3? true

Watch out

Single quotes mean one char. Double quotes mean a String. True and false are written in lower case. And booleans will be the heart of decisions, in module four.

keyvalue
'A'a char
"A"a String
true / falselower case
Coming soondecisions with if

Summary

A char is one character in single quotes, and a boolean is true or false. Next, converting between types.

Key takeaways
  • char: 'A'
  • boolean: true, false
  • Comparisons give booleans
Next up: Type Casting — converting between types