Avoid using x % 2 == 1 or x % 2 > 0 to check whether a number x is odd, or x % 2 != 1 to check whether it is even. Such code does not work for negative numbers. For example, -5 % 2 equals -1, not 1.

Consider using x % 2 != 0 to check for odd and x % 2 == 0 to check for even.

-9 is an odd number but this example does not detect it as one. This is because -9 % 2 is -1, not 1.

It would be better to check if the number is even and then invert that check.

  • J. Bloch and N. Gafter, Java Puzzlers: Traps, Pitfalls, and Corner Cases, Puzzle 1. Addison-Wesley, 2005.
  • Java Language Specification: Remainder Operator %.