Is it good practice to use the XOR (^) operator in Java for boolean checks?
- by Pete
I personally like the 'exclusive or' operator when it makes sense in context of boolean checks because of its conciseness. I much prefer to write
if (boolean1 ^ boolean2)
{
//do it
}
than
if((boolean1 && !boolean2) || (boolean2 && !boolean1))
{
//do it
}
but I often get confused looks (from other experienced java developers, not just the newbies), and sometimes comments about how it should only be used for bitwise operations. I'm curious as to the best practices others use around the '^' operator.