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
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:
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?
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:
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:
int count = 1;
if (count) { } // error: incompatible types: int cannot be converted to boolean
if (count != 0) { } // correctAlso, 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?
- true
- false
- 5
- Compile error
Which statement about Java booleans is correct?
- 0 means false and 1 means true
- A boolean can be true, false or null
- A boolean is either true or false
- Booleans are stored as strings
What does !(10 == 10) evaluate to?
- true
- false
- 10
- Error