Explain OR & AND Operator In Java ?

OR Operator in Java:

Definition:

In Java, the OR operator is represented by || (logical OR) or | (bitwise OR). The logical OR (||) evaluates two boolean expressions and returns true if at least one of the operands is true. The bitwise OR (|) operates on individual bits of two operands and returns 1 in each bit position where at least one of the bits is 1.

Example:

Logical OR (||):

public class LogicalORExample {
    public static void main(String[] args) {
        int a = 5, b = 10;

        // Logical OR
        if (a > 0 || b > 0) {
            System.out.println("At least one of 'a' or 'b' is positive.");
        }

        // Short-circuit behavior
        if (a > 0 || ++b > 10) {
            System.out.println("Short-circuited; 'b' is still " + b);
        }
    }
}

Output:

At least one of 'a' or 'b' is positive.
Short-circuited; 'b' is still 10

Bitwise OR (|):

public class BitwiseORExample {
    public static void main(String[] args) {
        int x = 5; // 0101 in binary
        int y = 3; // 0011 in binary

        // Bitwise OR
        int result = x | y; // 0111 in binary -> 7 in decimal
        System.out.println("Bitwise OR result: " + result);
    }
}

Output:

Bitwise OR result: 7

Features of OR Operator:

  • Short-circuits, meaning if the first operand evaluates to true, the second operand is not evaluated.
  • Always evaluates both operands and operates on binary representations of numbers.

Advantages:

  • Efficient with short-circuit evaluation when only one condition is needed to be true.
  • Efficient in manipulating individual bits in data, commonly used in low-level programming.

Uses:

  • Used in conditional statements where multiple conditions need to be checked.
  • Used in bit manipulation, networking, flags, and low-level operations.

AND Operator in Java:

Definition:

In Java , the AND operator is represented by && (logical AND) or & (bitwise AND). The logical AND (&&) checks two boolean expressions and returns true if both operands are true. The bitwise AND (&) operates on individual bits and returns 1 in each bit position where both bits are 1.

Example:

Logical AND (&&):

public class LogicalANDExample {
    public static void main(String[] args) {
        int a = 5, b = 10;

        // Logical AND
        if (a > 0 && b > 0) {
            System.out.println("Both 'a' and 'b' are positive.");
        }

        // Short-circuit behavior
        if (a < 0 && ++b > 10) {
            System.out.println("This won't execute; 'b' is " + b);
        } else {
            System.out.println("Short-circuited; 'b' remains " + b);
        }
    }
}

Output:

Both 'a' and 'b' are positive.
Short-circuited; 'b' remains 10

Bitwise AND (&):

public class BitwiseANDExample {
    public static void main(String[] args) {
        int x = 5; // 0101 in binary
        int y = 3; // 0011 in binary

        // Bitwise AND
        int result = x & y; // 0001 in binary -> 1 in decimal
        System.out.println("Bitwise AND result: " + result);
    }
}

Output:

Bitwise AND result: 1

Features of AND Operator:

  • Short-circuits, meaning if the first operand is false, the second operand is not evaluated.
  • Always evaluates both operands and operates on binary representations of numbers.

Advantages:

  • Helps in improving performance through short-circuit evaluation when both conditions must be true.
  • Efficient in masking and clearing specific bits.

Uses:

  • Used in conditional statements where multiple conditions must be met.
  • Used in bit masking, checksum calculations, and low-level programming for managing data.

0 Shares:
You May Also Like