Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the logical xor operator in java? [duplicate]

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.

like image 880
Pokechu22 Avatar asked Oct 15 '14 19:10

Pokechu22


1 Answers

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.

like image 175
NPE Avatar answered Oct 17 '22 17:10

NPE