Logical Operators are used to evaluate the outcome of conditions. There are three logical operators in java: AND (&&), OR (||) and NOT (!). The AND and OR operators are used when multiple conditions are combined and we need to evaluate the outcome as a whole.
AND Operator: It returns true if all the conditions are true.
OR Operator: It returns true if any of the condition is true.
NOT Operator: It returns true if condition is false, returns false if condition is true.
This can be represented by a truth table like this:
Logical ‘AND’ Operator (&&) Example
Let’s say, an institute grants admission based on the student marks. If student math marks represented by variable mathVar
and science marks represented by scienceVar
both are greater than 95 then the admission is granted, else the admission is not granted.
This condition can be written using AND operator and if-else statement like this:
class JavaExample { public static void main(String[] args) { int mathVar = 94, scienceVar =99; //checking whether marks in both the subjects are //greater than 95 if (mathVar >=95 && scienceVar>=95){ System.out.println("Admission granted."); } else{ System.out.println("Sorry! Admission is not granted."); } } }
Output: Admission is not granted because both the conditions are not true.
Logical ‘OR’ Operator (||) Example
Case 2: Let’s say, another institute grants admission if marks in any one of the subject is greater than 98. This means, if student scored more than 98 in any one of the subject then the admission is granted in this institute. In this case, we can use OR Operator as we need any one of the condition to return true.
class JavaExample { public static void main(String[] args) { //let's consider the marks of the same student that we have seen //in the first example int mathVar = 94, scienceVar =99; //checking whether marks in any one of the subject is greater //than 98 if (mathVar >=98 || scienceVar>=98){ System.out.println("Admission granted."); } else{ System.out.println("Sorry! Admission is not granted."); } } }
Output:
Logical ‘NOT’ Operator (!) Example
Case 3: There is a third institute that grants admission, if marks in math subject is not less than 75. Let’s write this case using NOT Operator.
class JavaExample { public static void main(String[] args) { int mathVar = 94, scienceVar =99; //In this case, marks in science subject is not required //to be checked. if (!(mathVar<75)){ System.out.println("Admission granted."); } else{ System.out.println("Sorry! Admission is not granted."); } } }
Output:
Admission granted.
Short-circuit in Logical Operators
Short circuit means that the compiler doesn’t evaluate the right side condition if it is not necessary. This happens while evaluating the conditions using && and || operators.
In every AND and OR operators operation, both sides are evaluated except the following case:
If first condition returns false in AND operation:
false && …… : it is not necessary to evaluate the right-hand side because the result can only be false regardless of the condition on right side. This is because AND returns false, if any one of the condition is false.
If first condition returns true in OR operation:
true || ……. : it is not necessary to evaluate the right-hand side because the result can only be true regardless of the conditions on the right side. This is because OR returns true, if any one of the condition is true.
Short Circuit Example
In this example, you would think that a compiler checks for both the conditions 1==2
and 5+5==10
. However this is not happening, when compiler checks for first condition and finds the result false, it knows that the output for whole expression cannot be true whatever the outcome of second condition so it doesn’t check the second condition.
class JavaExample { public static void main(String[] args) { if(1==2 && 5+5 ==10){ System.out.println("Hello"); }else{ System.out.println("Bye"); } } }