TExcelTAD
Home › Java › Java Booleans

Java Booleans

Programs constantly have to answer yes/no questions: Is the user logged in? Is the cart empty? Did the payment succeed? Java represents such answers with the boolean type, which has exactly two values: true and false.

Boolean variables

java · try it yourself
class Main {
    public static void main(String[] args) {
        boolean isLoggedIn = true;
        boolean hasNewMessages = false;
        System.out.println(isLoggedIn);
        System.out.println(hasNewMessages);
    }
}
true
false

Tip: Name booleans so they read like a question: isEmpty, hasAccess, canEdit, shouldRetry.

Boolean expressions

Comparison operators produce boolean values. You can print them, store them, or use them directly in if statements and loops:

java · try it yourself
class Main {
    public static void main(String[] args) {
        int temperature = 28;
        int limit = 25;
        boolean isHot = temperature > limit;

        System.out.println(temperature > limit);
        System.out.println(temperature == 28);
        System.out.println(10 <= 9);
        System.out.println("isHot = " + isHot);
    }
}
true
true
false
isHot = true

Combining conditions

Expression a = true, b = false
a && b (AND) false
a || b (OR) true
!a (NOT) false
a ^ b (XOR — exactly one is true) true

A small real-world example — can a person vote?

java · try it yourself
class Main {
    public static void main(String[] args) {
        int age = 19;
        boolean isCitizen = true;
        boolean isRegistered = false;

        boolean canVote = age >= 18 && isCitizen && isRegistered;
        boolean needsAction = age >= 18 && isCitizen && !isRegistered;

        System.out.println("Can vote: " + canVote);
        System.out.println("Should register: " + needsAction);
    }
}
Can vote: false
Should register: true

Methods that return booleans

Many library methods answer questions with a boolean. They're often named isX, hasX, canX, contains or equals:

java · try it yourself
class Main {
    public static void main(String[] args) {
        String email = "ada@example.com";
        System.out.println(email.contains("@"));
        System.out.println(email.endsWith(".org"));
        System.out.println(email.isEmpty());
        System.out.println(Character.isDigit('7'));
        System.out.println(Character.isUpperCase('a'));
        System.out.println(Boolean.parseBoolean("TRUE"));
    }
}
true
false
false
true
false
true

Boolean.parseBoolean returns true only for the text "true" (ignoring case); anything else gives false.

Booleans are not numbers

Unlike C, Java does not treat 0 as false or 1 as true. An int can't be used where a boolean is expected:

java
int count = 1;
if (count) { }          // error: incompatible types: int cannot be converted to boolean
if (count != 0) { }     // correct

Also, there's no need to compare a boolean with true. if (isReady == true) works, but if (isReady) says the same thing more clearly, and if (!isReady) replaces if (isReady == false).

Test yourself

What is the value of 5 > 3 && 2 > 4?

  1. true
  2. false
  3. 5
  4. Compile error

Which statement about Java booleans is correct?

  1. 0 means false and 1 means true
  2. A boolean can be true, false or null
  3. A boolean is either true or false
  4. Booleans are stored as strings

What does !(10 == 10) evaluate to?

  1. true
  2. false
  3. 10
  4. Error