Creating a “logical exclusive or” operator in Java

Java does have a logical XOR operator, it is ^ (as in a ^ b).

Apart from that, you can’t define new operators in Java.

Edit: Here’s an example:

public static void main(String[] args) {
    boolean[] all = { false, true };
    for (boolean a : all) {
        for (boolean b: all) {
            boolean c = a ^ b;
            System.out.println(a + " ^ " + b + " = " + c);
        }
    }
}

Output:

false ^ false = false
false ^ true = true
true ^ false = true
true ^ true = false

Leave a Comment

tech