How to create a Logical Operator in Java
Exclusive “or” (xor
) Logical Operator
In some scripting languages like PHP, there exists a logical operator (e.g. &&
, ||
, and
, or
, etc.) called the “Exclusive Or”. The exclusive or evaluates two booleans. It then returns true if exactly one of the two expressions are true, false otherwise. For example:
|
|
How to create a Logical Operator in Java
As Java does not ship out the box with this functionality, we can easily create it ourselves.
To create an xor
logical operator
, or exclusive or
as it is sometimes referred to, we can look to implement the following:
The solution in Java
In Java we can use the caret
character to perform an XOR operation.
Some test cases to validate our method
|
|