7

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.

Pokechu22
  • 4,984
  • 9
  • 37
  • 62

1 Answers1

22

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.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Interesting. The table at _The Java™ Tutorials_ on [Operators](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html) refers to "logical operators" as distinct from "bitwise operators", yet [Equality, Relational, and Conditional Operators](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html) refers to "conditional operators" as you say. – Garret Wilson Apr 27 '16 at 23:27
  • Ah, I think I understand the conflict. Actually I think that "logical operator" means that Boolean values are being compared. Thus the condition operators are logical operators, and the bitwise operators can also be used as logical operators. That's why the JLS says "Bitwise and Logical Operators" when referring to `&`, `|`, and `^`. So I don't think it would be incorrect to refer to `&&` and `||` (the conditional operators) as logical operators as well. – Garret Wilson Apr 27 '16 at 23:45