1. Boolean data type: boolean
As we have already seen, Java has a very useful operator—if-else. It executes one block of statements if the condition in parentheses is true, and a second block of statements if the condition is false.
To conveniently work with expressions that can be true or false, Java added a special type—boolean. Its key feature is that variables of this type can take only two values: true (true) and false (false).
It is impossible to assign any other values to variables of type boolean. The compiler will not allow it.
Examples of boolean variables:
boolean isAdult = true;
boolean hasMoney = false;
A variable of type boolean has only two values—true and false. That’s it! Almost like a test result—either “passed” or “failed.”
2. Comparison operations
So what is such a primitive type for? The real power of boolean shows when we build logical expressions. Usually they are formed using comparison operators.
| Operator | Purpose | Example | Value |
|---|---|---|---|
| == | Equal to | |
true or false |
| != | Not equal to | |
true or false |
| > | Greater than | |
true or false |
| < | Less than | |
true or false |
| >= | Greater than or equal to | |
true or false |
| <= | Less than or equal to | |
true or false |
Example:
int age = 20;
boolean isAdult = age >= 18; // true (20 is greater than or equal to 18)
boolean isTeenager = age >= 13 && age < 18; // false (20 is not less than 18)
boolean isEven = age % 2 == 0; // true (20 is divisible by 2 with no remainder)
isAdult will be true here because 20 is indeed an “adult” age.
Important point 1:
Operators consisting of two characters must not be split.
a < = 10
speed > = max
age = = 18
time ! = 0
Important point 2:
Note that the operators => and =< do not exist: there are only <= and >=. If you write a =< 3, your code simply won’t compile.
Important point 3:
In Java you cannot write an expression like 18 < age < 65. The expression 18 < age evaluates to true or false. And you cannot compare true with a number (65)—they are different types.
3. Logical operations
In Java you cannot write the expression 18 < age < 65—it is syntactically incorrect and the program won’t compile.
But you can write it like this:
(18 < age) AND (age < 65)
Of course, instead of the word AND we use logical operators. Java has three logical operators: AND (and), OR (or), and NOT (not).
Good news: using parentheses, you can build logical expressions of any complexity.
Bad news: in Java instead of the words and, or, and not, you use C-family symbols: &&, ||, !.
| Logical operator | Expectation | Reality |
|---|---|---|
| AND (∧) | and | && |
| OR (∨) | or | || |
| NOT (¬) | not | ! |
Here are a few examples of using logical operators in Java:
| Expression | Reading | Explanation |
|---|---|---|
|
(0 < a) and (a < 100) | (0 < a) AND (a < 100) |
|
(not a) and (not b) | (NOT a) AND (NOT b) |
|
not((not a) or (not b)) | NOT((NOT a) OR (NOT b)) |
4. Truth tables
Let’s briefly go over the logical operations.
The AND operator
The AND operator, aka &&, also called conjunction or logical multiplication.
| Expression | Result |
|---|---|
| true && true | true |
| true && false | false |
| false && true | false |
| false && false | false |
The OR operator
The OR operator, aka ||, also called disjunction or logical addition.
| Expression | Result |
|---|---|
| true || true | true |
| true || false | true |
| false || true | true |
| false || false | false |
The NOT operator
The NOT operator, aka !, also called inversion (negation).
| Expression | Result |
|---|---|
| ! true | false |
| ! false | true |
Useful expressions:
| Expression | Result |
|---|---|
| m && !m | false |
| m || !m | true |
| !(a && b) | !a || !b |
| !(a || b) | !a && !b |
5. Examples of logical operations: and, or, not
The “AND” operation is written as &&
The result is true only when both operands are true.
int age = 16;
boolean hasTicket = true;
boolean canAttend = age >= 14 && hasTicket; // true (16 is greater than 14 AND there is a ticket)
If you’re at least 14 AND you have a ticket—come on in!
The “OR” operation is written as (||)
The result is true if at least one condition is true.
boolean isAdmin = false;
boolean isModerator = true;
boolean canEdit = isAdmin || isModerator; // true (editing is allowed if at least one of these is true)
If you’re an admin OR a moderator—go ahead and press the buttons!
The “NOT” operation is written as (!)
The inverter turns true into false and vice versa.
boolean isWeekend = false;
boolean shouldGoToWork = !isWeekend; // true (if it’s not the weekend, we work)
If it’s NOT the weekend—it’s time to get up!
Combining: build conditions of any complexity
You can combine as many logical operations as you like (preferably without overdoing it). Parentheses can (and should!) be used for clarity.
Example:
int age = 17;
boolean hasTicket = false;
boolean isVip = true;
boolean canGo = (age >= 18 && hasTicket) || isVip; // true, because isVip = true
Logic: if a person is an adult and has a ticket, or if they are a VIP—let them in. If they are not a VIP—only if they are an adult and have a ticket.
GO TO FULL VERSION