Java Logical Operators

“The last think any sensible human being should want is immortality. As it is, life lasts too long for most of us” Joseph Heller

Before reading this section you should already be familiar with operators in general and comparison-operators.

When we want to check multiple conditions at the same time in a statement like if, we can use logical operators.

For example let’s say we have two variables named age and money. The type of both variables is integer. Now we want to run an if statement and see if the value of the age variable is higher than 21 and the value of the money variable is larger than 200. If both conditions were true, then only in that case the block of the if statement executes.

If you think about, in this example there are two conditions and we want both of them to result true and only in that case the body of the if statement runs.

This is where the logical operator can help.

Basically with the help of these operators we can involve more than one condition in a statement like if.

Here’s an introductory list of the Logical-operators:

Operator Symbol
AND Operator &&
OR Operator ||
Reverse the Result (Not) Operator !

AND `&&` Operator:

Via this operator we can bring at least two conditions into a statement like if and see if the result of those conditions are true.

If all comparisons involved in the && operator returned true, then the final result will be true as well.

Note: there’s no space between the two ampersands.

Example:

public class Simple {
public static void main(String[] args) {
int age = 21;
int money = 2000;
if (age >= 21 && money >= 500){
System.out.println("The person is allowed to enter to the bar.");
}else{
System.out.println("This person can't enter to the bar.");
}
}
}

Output: The person is allowed to enter to the bar.

As you can see, in the parentheses of the if-statement we involved two conditions. The first one is to check if the value of the age variable is higher or equal to 21 and the second condition is to check and see if the value of the money variable is higher or equal to 500.

Now because the result of the both conditions is true then the body of the if statement will run.

Note: the final result of this operator is true only if all the conditions involved return true. This means even if one condition returns false, the entire result will be false as well.

OR `||` Operator:

Just like AND operator, the use of OR || operator is to involve more than one condition in a statement like if.

But the difference however is that, this operator only looks for one condition to result true!

For example if there are 10 conditions involved and 9 out of 10 conditions resulted false but one was true, the end result is true as well.

Example:

public class Simple {
public static void main(String[] args) {
String job = "Java Developer";
if (job == "Java Developer" || job == "Python Developer"){
System.out.println("The person can apply for a position in this company.");
}else{
System.out.println("This person can't apply for a job in this company.");
}
}
}

Output:

The person can apply for a position in this company.

In this example, we’ve checked the value of the job variable to see if it matches “Java Developer” or “Python Developer”. If the value matches any of these two strings, the result of the if statement is true and so the body of this statement will be executed.

Reverse the Result (AKA Not) `!` Operator:

This operator is used to reverse the result of a condition. It sits on the left side of a condition and if for example the condition results true, it will become false and vice versa.

Example:

public class Simple {
public static void main(String[] args) {
int age = 25;

if (!(age > 18) ){
System.out.println("The age is higher than 18.");
}else{
System.out.println("The person's age is not above 18.");
}
}
}

Output: The person’s age is not above 18.

In the example above, even though the number assigned to the age variable is higher than 18, the condition of the if statement results false and this is because we reversed the result by adding the ! operator on the left side of the condition. So the body of the if-statement is skipped and the body of the else statement ran instead.