Java OR

Java OR Operator is used to perform logical OR operation between two boolean operands.

OR Operator is usually used in creating complex conditions like combining two or more simple conditions.

OR Operator Symbol

The symbol used for OR Operator is ||.

Syntax

The syntax to use OR Operator with operands a and b is

</>
Copy
a || b

OR Operator supports chaining. For example, the syntax to write a condition with four boolean values is

</>
Copy
a || b || c || d

The above expressions returns true if any of the operands is true.

OR Truth Table

The following truth table provides the output of OR operator for different values of operands.

aba || b
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

OR Operation returns true, if any of the operands is true, else, it returns false.

Examples

In the following example, we take two boolean variables with different combinations of true and false, and find their logical OR output.

Main.java

</>
Copy
public class Main {
    public static void main(String[] args) {
        boolean a, b, result;
        
        a = true;
        b = true;
        result = (a || b);
        System.out.println(a + "  || " + b + "  = " + result);
        
        a = true;
        b = false;
        result = (a || b);
        System.out.println(a + "  || " + b + " = " + result);

        a = false;
        b = true;
        result = (a || b);
        System.out.println(a + " || " + b + "  = " + result);

        a = false;
        b = false;
        result = (a || b);
        System.out.println(a + " || " + b + " = " + result);
    }
}

Output

true  || true  = true
true  || false = true
false || true  = true
false || false = false

In the following example, we use logical OR Operator in If Statement’s Condition to combine two simple conditions to form a compound condition.

index.html

public class Main {
    public static void main(String[] args) {
        int a = 3;
        if (a % 2 == 0 || a > 0) {
            System.out.println("true.");
        } else {
            System.out.println("false.");
        }
    }
}

Output

true.

The first condition a % 2 == 0 is false and the second condition a > 0 is true. false or true is true.

Conclusion

In this Java Tutorial, we learned about Logical OR Operator, it’s syntax, and usage with examples.