Introduction
Logical operators in Java are used to perform logical operations on boolean expressions. These operators are typically used in control flow statements like if
, while
, and for
loops, where decisions are made based on multiple conditions. Java provides three main logical operators: AND (&&
), OR (||
), and NOT (!
). These operators help in combining multiple boolean expressions or inverting the result of a boolean expression.
Key Points:
- Boolean Expressions: Logical operators operate on boolean expressions and return a boolean value (
true
orfalse
). - Control Flow: Commonly used in conditional statements to control the flow of the program.
- Short-Circuiting: The AND (
&&
) and OR (||
) operators exhibit short-circuit behavior, which means they stop evaluating expressions as soon as the result is determined.
List of Logical Operators
Java provides the following logical operators:
Operator | Description | Example | Output |
---|---|---|---|
&& |
Logical AND | (a > b) && (c > d) |
true if both expressions are true |
|| |
Logical OR | (a > b) || (c > d) |
true if at least one expression is true |
! |
Logical NOT | !(a > b) |
Inverts the result of the expression |
Detailed Examples of Logical Operators
Let’s explore each logical operator with examples.
1. Logical AND (&&)
The logical AND operator &&
returns true
if both operands are true. If either of the operands is false, the result is false
. This operator is often used when multiple conditions need to be true for a certain block of code to execute.
public class LogicalAndExample {
public static void main(String[] args) {
int a = 5;
int b = 10;
int c = 15;
boolean result = (a < b) && (b < c); // true, since both conditions are true
System.out.println("(a < b) && (b < c): " + result); // Output: (a < b) && (b < c): true
}
}
Explanation:
- Condition 1:
a < b
istrue
. - Condition 2:
b < c
istrue
. - Result: Since both conditions are true, the overall result of the logical AND operation is
true
.
Output:
(a < b) && (b < c): true
2. Logical OR (||)
The logical OR operator ||
returns true
if at least one of the operands is true. If both operands are false, the result is false
. This operator is used when at least one of multiple conditions needs to be true for a block of code to execute.
public class LogicalOrExample {
public static void main(String[] args) {
int a = 5;
int b = 10;
int c = 15;
boolean result = (a > b) || (b < c); // true, since one condition is true
System.out.println("(a > b) || (b < c): " + result); // Output: (a > b) || (b < c): true
}
}
Explanation:
- Condition 1:
a > b
isfalse
. - Condition 2:
b < c
istrue
. - Result: Since at least one condition is true, the overall result of the logical OR operation is
true
.
Output:
(a > b) || (b < c): true
3. Logical NOT (!)
The logical NOT operator !
inverts the value of a boolean expression. If the expression is true
, it becomes false
, and if it is false
, it becomes true
. This operator is used when you need to reverse the result of a boolean expression.
public class LogicalNotExample {
public static void main(String[] args) {
int a = 5;
int b = 10;
boolean result = !(a > b); // true, since a > b is false and !false is true
System.out.println("!(a > b): " + result); // Output: !(a > b): true
}
}
Explanation:
- Condition:
a > b
isfalse
. - Inversion: The
!
operator invertsfalse
totrue
. - Result: The overall result of the logical NOT operation is
true
.
Output:
!(a > b): true
Short-Circuiting in Logical Operators
Short-Circuit AND (&&)
The logical AND operator &&
short-circuits, meaning that if the first operand is false, the second operand is not evaluated because the result is already determined to be false.
public class ShortCircuitAndExample {
public static void main(String[] args) {
int a = 5;
int b = 10;
boolean result = (a > b) && (++b > 10); // The increment operation is not performed
System.out.println("Result: " + result); // Output: Result: false
System.out.println("Value of b: " + b); // Output: Value of b: 10
}
}
Explanation:
- Short-Circuit: Since
a > b
isfalse
, the second condition++b > 10
is not evaluated, andb
remains 10.
Output:
Result: false
Value of b: 10
Short-Circuit OR (||)
The logical OR operator ||
short-circuits, meaning that if the first operand is true, the second operand is not evaluated because the result is already determined to be true.
public class ShortCircuitOrExample {
public static void main(String[] args) {
int a = 5;
int b = 10;
boolean result = (a < b) || (++b > 10); // The increment operation is not performed
System.out.println("Result: " + result); // Output: Result: true
System.out.println("Value of b: " + b); // Output: Value of b: 10
}
}
Explanation:
- Short-Circuit: Since
a < b
istrue
, the second condition++b > 10
is not evaluated, andb
remains 10.
Output:
Result: true
Value of b: 10
Conclusion
Java’s logical operators are essential for making decisions in your code by evaluating multiple conditions. They allow you to control the flow of your program based on complex boolean expressions.
Summary:
- Logical AND (
&&
): Returnstrue
only if both operands are true. Exhibits short-circuit behavior. - Logical OR (
||
): Returnstrue
if at least one operand is true. Exhibits short-circuit behavior. - Logical NOT (
!
): Inverts the value of a boolean expression, turningtrue
intofalse
and vice versa.