Search Logic Blocks

Wednesday, January 8, 2020

Java: Logical Operators

In last post, we learnt about relational / comparison operators. Relational operators always return a boolean value - true or false. Logical operators operate on boolean values (or on expressions which return boolean values) and  return again boolean value - true or false.

Logical AND (&) operator
& operator is performed on two boolean values and if both values are true, then only it returns true, else it returns false. Here is the chart of Logical AND (&) operator -

First Value Second Value Result
true true true
true false false
false true false
false false false

Logical OR (|) operator
| operator is performed on two boolean values and if either of both values is true, then it returns true, else it returns false. Here is the chart of Logical OR (|) operator -

First Value Second Value Result
true true true
true false true
false true true
false false false


Logical XOR (^) operator
^ operator is performed on two boolean values and if both values are same (either true or false), then it returns false, else it returns true. It is also called exclusive OR operator. Here is the chart of Logical XOR (^) operator -

First ValueSecond ValueResult
truetruefalse
truefalsetrue
falsetruetrue
falsefalsetrue

Logical NOT (!) operator
! operator is performed on one boolean value and returns its opposite value. Here is the chart of Logical NOT (!) operator -

Value Result
true false
false true

Here is the example:
boolean bln1, bln2, bln3;

int x = 45, y = 70, z = 65;
bln1 = (x > y); // will return false

bln2 = (y > z); // will return true

bln3 = (bln1 & bln2); // will return false
bln3 = (bln1 | bln2); // will return true
bln3 = (!bln1); // will return true
If we change the values in above example, there will be different results:
boolean bln1, bln2, bln3;

int x = 25, y = 10, z = 8;
bln1 = (x > y); // will return true

bln2 = (y > z); // will return true

bln3 = (bln1 & bln2); // will return true
bln3 = (bln1 | bln2); // will return true
bln3 = (!bln1); // will return false
Short-circuit Logical Operators
We use && (AND) and || (OR) operators with the complex expressions. These operators are also called short-circuit operators. In above example, we can write these three statements in one statement:
bln1 = (x > y); // will return true
bln2 = (y > z); // will return true
bln3 = (bln1 & bln2); // will return true
// This one statement is same as above 3 lines

bln3 = (x > y) && (y > z);
Why are these operators are called short-circuit? In && operator, if the first operand value is  false, it won't evaluate another operand as the outcome will always be false. In same way, in || operator, if the first operand value is true, it won't evaluate second operand as the outcome is always be true. 

NOTE: By using short-circuit operators, we are saving time and writing efficient code. Short-circuit operators are also called conditional-and and conditional-or operators.

Example : What should be the output of the following java code?

Class Logical (Logical.java)
public class Logical {
    public static void main(String args[]) {
        int x = 30, y = 40, z = 50;

        boolean bln1, bln2, bln3;

        bln1 = (x >= y);
        bln2 = (y <= z);
        bln3 = bln1 || bln2;

        System.out.println("First Result Is: " + bln3);

        // Let's change values and expressions        x *= 5;
        y += 5;
        z -= 10;

        bln1 = (x != y);
        bln2 = (y < z);
        bln3 = bln1 && bln2;
        System.out.println("Second Result Is: " + bln3);

        bln3 = !bln1;
        System.out.println("Third Result Is: " + bln3);
    }
}
First Result
bln1 = (x >= y) => (30 >= 40) => false
bln2 = (y <= z) => (40 <= 50) => true
bln3 = bln1 | bln2 => false | true => true
Second Result
x *= 5 => x = x * 5 => x = 30 * 5 = 150
y += 5 => y = y + 5 => y = 40 + 5 = 45
z -= 10 => z = z - 10 = 50 - 10 = 40

bln1 = (x != y) => (150 != 45) => true
bln2 = (y < z) => (45 < 40) => false
bln3 = bln1 & bln2 => true & false => false
Third Result
bln3 = !bln1 = !true = false

First Result Is: true
Second Result Is: false
Third Result Is: false

No comments: