Java Booleans
A Java boolean is a data type that can store only one of two values: true or false. Booleans are used when a program needs to answer a yes-or-no question.
You will see booleans anywhere Java makes a decision, such as checking whether a score passed, whether a user is logged in, or whether a number is greater than another number.
Boolean Values
The boolean type is written as boolean. Its only possible values are the keywords true and false, written in lowercase.
public class Main {
public static void main(String[] args) {
boolean isJavaFun = true;
boolean isFinished = false;
System.out.println("Java is fun: " + isJavaFun);
System.out.println("Lesson finished: " + isFinished);
}
}
Output:
Java is fun: true
Lesson finished: false
How It Works
The variable isJavaFun stores true, and isFinished stores false. When a boolean is printed, Java displays the word true or false.
Boolean variable names often start with words like is, has, or can. This makes the variable read like a question: isFinished, hasAccess, or canVote.
Booleans From Comparisons
Most boolean values in real programs come from expressions. A comparison expression checks two values and produces a boolean result.
| Operator | Meaning | Example |
|---|---|---|
== |
Equal to | age == 18 |
!= |
Not equal to | score != 0 |
> |
Greater than | score > 90 |
< |
Less than | temperature < 32 |
>= |
Greater than or equal to | score >= 70 |
<= |
Less than or equal to | items <= 10 |
The next program stores the results of comparisons in boolean variables.
public class Main {
public static void main(String[] args) {
int score = 82;
int passingScore = 70;
int attempts = 3;
boolean passed = score >= passingScore;
boolean perfectScore = score == 100;
boolean outOfAttempts = attempts >= 3;
System.out.println("Passed: " + passed);
System.out.println("Perfect score: " + perfectScore);
System.out.println("Out of attempts: " + outOfAttempts);
}
}
Output:
Passed: true
Perfect score: false
Out of attempts: true
The expression score >= passingScore is true because 82 is greater than 70. The expression score == 100 is false because 82 is not equal to 100.
Combining Boolean Expressions
Logical operators let you combine boolean expressions. Use && when both conditions must be true, || when at least one condition must be true, and ! to reverse a boolean value.
public class Main {
public static void main(String[] args) {
int age = 20;
boolean hasTicket = true;
boolean isBanned = false;
boolean oldEnough = age >= 18;
boolean canEnter = oldEnough && hasTicket && !isBanned;
System.out.println("Old enough: " + oldEnough);
System.out.println("Can enter: " + canEnter);
}
}
Output:
Old enough: true
Can enter: true
In this example, canEnter is true because the person is old enough, has a ticket, and is not banned. The ! operator changes isBanned from false to true for that part of the condition.
Common Mistakes
- Writing
TrueorFalsewith uppercase letters. Java requirestrueandfalse. - Using
=when you mean==. A single=assigns a value;==compares values. - Putting boolean values in quotes.
trueis a boolean, but"true"is a String. - Forgetting that
&&requires every condition to be true.
Takeaway: booleans store true-or-false values, and comparison expressions are the main way Java programs create conditions for decisions.
