Java Logical And Or Xor Examples



Write a Java program that applies the logical And/Or/Xor to two boolean expressions.
Check out Java 8 examples at Java 8 Examples
Checkout all Java programs at 100+ Java Programs 

Apply logical And/Or/Xor to two booleans

package net.sourcecodeexamples.java;

public class Main {

    public static void main(String[] args) {

        int s = 10;
        int m = 21;

        // if (s > m && m < 50)
        if (Boolean.logicalAnd(s > m, m < 50)) {
            System.out.println("Boolean.logicalAnd returned true");
        } else {
            System.out.println("Boolean.logicalAnd returned false");
        }

        // if (s > m || m < 50)
        if (Boolean.logicalOr(s > m, m < 50)) {
            System.out.println("Boolean.logicalOr returned true");
        } else {
            System.out.println("Boolean.logicalOr returned false");
        }

        // if (s > m ^ m < 50)
        if (Boolean.logicalXor(s > m, m < 50)) {
            System.out.println("Boolean.logicalXor returned true");
        } else {
            System.out.println("Boolean.logicalXor returned false");
        }

        if (Boolean.logicalAnd(Boolean.logicalOr(s > m, m < 50), Boolean.logicalOr(s <= m, m > 50))) {
            System.out.println("Combination returned true");
        } else {
            System.out.println("Combination returned false");
        }
    }
}

Output

Boolean.logicalAnd returned false
Boolean.logicalOr returned true
Boolean.logicalXor returned true
Combination returned true
Check out Java 8 examples at Java 8 Examples
Checkout all Java programs at 100+ Java Programs 

Comments