Logical Operators in Java
Logical operators in Java are used to combine multiple conditions and perform logical operations. They are mainly used with boolean values and return a result as either true or false.
Logical operators are commonly used in decision-making statements like if, if-else, and loops to create complex conditions in Java programs.
What are Logical Operators in Java?
Logical operators are special symbols that allow programmers to combine multiple relational expressions. They help in checking more than one condition at the same time.
For example, if you want to check whether a user is above 18 years old and has a valid ID, you can use the logical AND operator (&&).
Syntax of Logical Operators in Java
The basic syntax of logical operators is:
condition1 operator condition2;
Here, the operator combines two or more conditions and produces a boolean result.
Example of Logical Operator in Java
public class Main {
public static void main(String[] args) {
int age = 20;
boolean hasId = true;
System.out.println(age >= 18&& hasId);
}
}
Types of Logical Operators in Java
Java provides three main logical operators used for combining and modifying conditions.
- Logical AND Operator (&&)
- Logical OR Operator (||)
- Logical NOT Operator (!)
Java Logical Operators Table
| Operator | Name | Description |
|---|---|---|
| && | Logical AND | Returns true when all conditions are true |
| || | Logical OR | Returns true when at least one condition is true |
| ! | Logical NOT | Reverses the boolean result |
Logical AND Operator (&&) in Java
The logical AND operator (&&) is used to combine two or more conditions. It returns true only when all conditions are true. If any one condition is false, the result will be false.
The AND operator is commonly used when multiple conditions must be satisfied at the same time.
int age = 22;
boolean license = true;
System.out.println(age >= 18 && license);
Logical OR Operator (||) in Java
The logical OR operator (||) is used when at least one condition needs to be true. It returns true if any one of the conditions is true.
The result will be false only when all conditions are false.
int marks = 35;
boolean extraClass = true;
System.out.println(marks >= 40 || extraClass);
Logical NOT Operator (!) in Java
The logical NOT operator (!) is used to reverse the result of a boolean expression. If the condition is true, NOT changes it to false, and if it is false, NOT changes it to true.
boolean status = true;
System.out.println(!status);
Logical Operators Truth Table in Java
AND Operator (&&) Truth Table
| Condition 1 | Condition 2 | Result |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
OR Operator (||) Truth Table
| Condition 1 | Condition 2 | Result |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
NOT Operator (!) Truth Table
| Condition | Result |
|---|---|
| true | false |
| false | true |
Using Logical Operators with if Statement in Java
Logical operators are mostly used with conditional statements to execute code based on multiple conditions.
int age = 25;
boolean verified = true;
if (age >= 18 && verified) {
System.out.println("Access Granted");
}
Advantages of Logical Operators in Java
Multiple Conditions
Logical operators allow developers to combine multiple conditions in a single expression.
Decision Making
They help create powerful decision-making logic using if and conditional statements.
Better Control
Logical operators provide better control over program execution based on conditions.
Easy Readability
They make complex conditions shorter and easier to understand.
Common Mistakes While Using Logical Operators
- Using single & instead of logical AND operator &&.
- Using single | instead of logical OR operator ||.
- Forgetting that logical operators work with boolean expressions.
- Writing complex conditions without proper brackets.
Difference Between Logical AND (&&) and Bitwise AND (&)
| Logical AND (&&) | Bitwise AND (&) |
|---|---|
| Used with boolean conditions. | Used for bit-level operations. |
| Stops checking when result is known (short-circuit). | Evaluates all operands. |
| Example: a > 10 && b < 20 | Example: a & b |
Example of Multiple Logical Operators in Java
int age = 21;
boolean student = true;
boolean discount = false;
System.out.println(age >= 18 && student || discount);
Summary
Logical operators in Java are used to combine multiple conditions and perform logical operations. They return boolean values such as true or false.
Java provides three main logical operators: AND (&&), OR (||), and NOT (!). These operators are widely used in conditional statements and loops.
Understanding logical operators is important for creating efficient decision making logic in Java applications.