In programming, you often need a data type that can only have one of two values, like YES / NO, ON / OFF, or TRUE / FALSE. In Java, we use the boolean data type for this.
true and false.
A boolean type is declared with the boolean keyword and can only take the values true or false. This is the foundation of all conditional logic in Java.
Common uses for booleans:
if-else statements.isLoggedIn).A Boolean expression returns a boolean value: true or false. You can use comparison operators, such as the greater than (>) operator, to find out if an expression is true.
Understanding how to combine boolean values is key to building complex logic:
Checks if two values are equal. For example, 10 == 10 returns true.
Returns true only if both statements are true. (e.g., x < 5 && x < 10)
Returns true if at least one of the statements is true.
While the primitive boolean is simple, the Boolean wrapper class provides utility methods:
| A | B | A && B (AND) | A || B (OR) |
|---|---|---|---|
| True | True | True | True |
| True | False | False | True |
| False | False | False | False |
Booleans are the brain of your program. Without them, your code wouldn't be able to make decisions or handle different scenarios dynamically.
Next: Learn If...Else Statements →