Summary: in this tutorial, you will learn how to use the Java logical operators to perform logical operations on boolean values.
Introduction to Java logical operators
Logical operators allow you to perform logical operations on boolean values. Typically, you use the logical operators to combine boolean expressions to make decisions in programs.
Java supports three logical operators:
Logical Operator | Meaning |
---|---|
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
Logical AND (&&)
Java uses && to denote the logical AND operator. The logical AND operator combines two boolean expressions and returns true only if both expressions are true. If either or both expressions are false, the result is false.
Here’s the syntax for using the logical AND operator:
boolean result = expression1 && expression2;
Code language: Java (java)
The following table illustrates the value of the result depending on the values of the expression1 and expression2:
expression1 | expression2 | expression1 && expression2 |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
The following example shows how to use the logical AND operator:
public class App {
public static void main(String[] args) {
boolean isSunday = true;
boolean isCool = true;
boolean isPerfectDay = isCool && isSunday;
System.out.println(isPerfectDay); // true
}
}
Code language: Java (java)
Output:
true
Code language: Java (java)
In this example, isPerfectDay
is be true
only because both isSunny
and isWarm
are true
.
Logical OR (||)
The logical OR operator combines two boolean expressions and returns true if at least one of the expressions is true. It returns false only if both expressions are false.
Java uses the || to denote the logical OR operator.
Here’s the syntax for using the logical OR operator:
boolean result = expression1 || expression2;
Code language: Java (java)
The following table illustrates the value of the logical OR operator depending on the values of the expressions:
expression1 | expression2 | expression1 || expression2 |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
The following example shows how to use the logical OR operator:
public class App {
public static void main(String[] args) {
boolean hasCoffee = true;
boolean hasTea = false;
boolean isCaffeinated = hasCoffee || hasTea;
System.out.println(isCaffeinated); // true
}
}
Code language: Java (java)
Output:
true
Code language: Java (java)
In this example, isCaffeinated
is true
because hasCoffee
is true
, even though hasTea
is false
.
Logical NOT (!)
The logical NOT operator accepts one boolean expression and returns true if the expression is false and false if the expression is true. In other words, the logical NOT operator negates the value of the boolean expression.
Java uses the ! to denote the logical NOT operator.
The following table shows the result of the logical NOT operator:
expression | !expression |
---|---|
true | false |
false | true |
The following example illustrates how to use the logical NOT operator:
public class App {
public static void main(String[] args) {
boolean isRaining = true;
boolean isNotRaining = !isRaining;
System.out.println(isNotRaining); // true
}
}
Code language: Java (java)
In this example, isNotRaining
is false
because isRaining
is true
, and the !
operator negates it.
Precedence of Java logical operators
If an expression contains multiple logical operators, Java evaluates them based on operator precedence, with higher precedence operators being evaluated before those with lower precedence.
The following shows the precedence of the logical operators from high to low:
- Logical NOT (!)
- Logical AND (&&)
- Logical OR (||)
To change the order of precedence of the logical operators, you use parentheses. For example:
boolean result = (true || false) && !(true && false);
Code language: Java (java)
In this example, Java evaluates the expression with parentheses first, and then the expression with the !, and the logical AND operator.
Summary
- Java logical operators perform logical operations on boolean values.
- The logical AND operator combines two boolean values and returns true if both expressions are true or false otherwise.
- The logical OR operator combines two boolean values and returns false if both expressions are false or true otherwise.
- The logical NOT operator negates a boolean value.