Search code examples
javaboolean-logicxor

What is the logical xor operator in java?


In java, there is a logical OR operator (||) and a logical AND operator (&&). Is there a logical XOR operator? I tried ^^ but it does not work.


Solution

  • The logical XOR operator does exist in Java and is spelled ^.

    To get the terminology right, in Java:

    • &, | and ^ are called bitwise or logical operators, depending on the types of their arguments;
    • && and || are called conditional operators.

    For details, see JLS § 15.22. Bitwise and Logical Operators onwards.

    There is no direct equivalent for && and || for XOR. The only reason && and || exist as separate operators from & and | is their short-circuiting behaviour (that's why they're called "conditional"), and XOR cannot be short-circuited.