Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

Authors Top

If you have a few years of experience in the Java ecosystem, and you’d like to share that with the community, have a look at our Contribution Guidelines.

1. Overview

In Java, when we work with types like Integer, Long, Float, and Double, we often want to check if the numbers are positive or negative. This is a fundamental and common number operation.

In this quick tutorial, we'll discuss how to check whether a given number is positive or negative.

2. Introduction to the Problem

Checking whether a number is positive or negative is a pretty straightforward problem. However, before we start looking at the implementations, let's understand the definition of positive and negative.

Given a real number n, if n is greater than zero, it's a positive number. Otherwise, if n is less than zero, it's negative. So, we still have a particular case: zero. Zero is neither positive nor negative.

So, we can create an enum to cover these three possibilities:

enum Result {
    POSITIVE, NEGATIVE, ZERO
}

In this tutorial, we'll address two different ways to check if a number is positive, negative, or zero. For simplicity, we'll use unit test assertions to verify the result.

So next, let's see them in action.

3. Using the ‘<‘ and the ‘>‘ Operators

Per definition, whether a number is positive or negative depends on the result of the comparison to zero. Therefore, we can use Java's “greater than (>)” and “less than (<)” operators to solve the problem.

Next, let's take the Integer type as an example to create a method to do the check:

static Result byOperator(Integer integer) {
    if (integer > 0) {
        return POSITIVE;
    } else if (integer < 0) {
        return NEGATIVE;
    }
    return ZERO;
}

The code above explains itself clearly. Depending on the result of the comparison to zero, we determine if the result is positive, negative, or zero.

Let's create a test to verify our method:

assertEquals(POSITIVE, PositiveOrNegative.byOperator(42));
assertEquals(ZERO, PositiveOrNegative.byOperator(0));
assertEquals(NEGATIVE, PositiveOrNegative.byOperator(-700));

Unsurprisingly, the test passes if we execute it.

Of course, the same logic works if we can change the Integer parameter to Long, Float, or Double.

4. Using the signum() Method

We've seen how to check if a number is positive or negative using the < and the > operators. Alternatively, we can use the signum() method to get the sign of the given number.

For Integer and Long numbers, we can call the Integer.signum() and Long.signum() methods.

The signum(n) method returns -1, 0, and 1 when n is negative, zero, or positive.

Let's take an Integer as an example to create a check method:

static Result bySignum(Integer integer) {
    int result = Integer.signum(integer);
    if (result == 1) {
        return Result.POSITIVE;
    } else if (result == -1) {
        return Result.NEGATIVE;
    }
    return Result.ZERO;
}

The test below verifies our method works as expected:

assertEquals(POSITIVE, PositiveOrNegative.bySignum(42));
assertEquals(ZERO, PositiveOrNegative.bySignum(0));
assertEquals(NEGATIVE, PositiveOrNegative.bySignum(-700));

Unlike Integer and Long, Float and Double classes don't provide the signum() method. However, the Math.signum() method accepts Float and Double numbers as the parameter, for example:

static Result bySignum(Float floatNumber) {
    float result = Math.signum(floatNumber);
   
    if (result.compareTo(1.0f) == 0) {
        return Result.POSITIVE;
    } else if (result.compareTo(-1.0f) == 0) {
        return Result.NEGATIVE;
    }
    return Result.ZERO;
}

Finally, let's create a test to verify if the method can check if a float number is positive or negative:

assertEquals(POSITIVE, PositiveOrNegative.bySignum(4.2f));
assertEquals(ZERO, PositiveOrNegative.bySignum(0f));
assertEquals(NEGATIVE, PositiveOrNegative.bySignum(-7.7f));

The test passes if we give it a run.

5. Conclusion

In this article, we've learned two ways to determine whether a given number is positive, negative, or zero.

As usual, all code snippets presented here are available over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
guest
3 Comments
Oldest
Newest
Inline Feedbacks
View all comments