Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:
Check if Two Integers Have the Same Sign
Last updated: September 19, 2025
1. Overview
In this tutorial, we’ll explore different ways to check if two integers have the same sign in Java. This is a common requirement in mathematical computations, validations, and optimization checks.
We’ll start with simple conditional logic and then move to more compact or efficient approaches.
2. Using Simple Conditional Checks
The most straightforward approach is to check whether both numbers are non-negative or both are negative:
int a = 5;
int b = -3;
boolean sameSign = (a >= 0 && b >= 0) || (a < 0 && b < 0);
Here, the condition explicitly verifies that either both values are non-negative or both are negative. While it might be a bit too explicit, simplistic, and considered to be non-elegant, this approach has a significant advantage: it works and it’s readable. However, we can wrap this logic inside a function to add clarity and improve reusability.
3. Using Multiplication
We can also rely on the product of two integers:
boolean sameSign = (a * b) > 0;
If the product is positive, then both numbers have the same sign. This approach isn’t very expressive and requires some time to understand what’s going on. Additionally, it has a problem with overflowing and multiplying by zero. While it’s a very inventive way to solve the problem, it’s too cryptic, prone to errors, and might affect the performance of an application.
4. Using Bitwise Operators
Another option is to compare the sign bits of the numbers, which is similar to the previous one, but doesn’t have the disadvantage of multiplication. In two’s complement representation, the sign of an integer is stored in the most significant bit:
boolean sameSign = (a ^ b) >= 0;
In this case, we’re applying an XOR operation to the bit that’s responsible for the sign: if they’re different, the bit would be set to one, and the final value would be negative. However, since bitwise operators are pretty rare, especially XOR, if we decide to go with this approach, the logic (even a single line) should be wrapped into a method.
5. Using Java’s Math.signum()
Java provides the Math.signum() method, which returns -1.0, 0.0, or 1.0 depending on the sign. This might be pretty useful in calculations:
boolean sameSign = Math.signum(a) == Math.signum(b);
This approach is very clear and avoids manual checks, but note that it operates on double values internally. Thus, to avoid additional conversions, it’s better to implement our own checks; fortunately, the logic is straightforward.
6. Handling Zeros
Depending on the context, we can consider zero to be either neutral or having the same sign as non-negative numbers. For example, zero and five may or may not be treated as the same sign. A reasonable option is to treat zero as non-negative, since it aligns with Math.signum(0) returning 0.0 and makes comparisons with positive numbers more intuitive. However, in strict mathematical contexts, you might prefer to handle zero separately to avoid ambiguity.
7. Conclusion
There are several ways to compare the signs of numbers. However, the best one is the one that would be the most readable and understandable in our codebase. Often, the simplest and dullest approach is what we need. If the checks become verbose, we can use encapsulation and move the logic to a separate method, making it even more readable and understandable.
The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
















