How to Count Positive and Negative Numbers in an Array in Java
Classifying numbers by their sign—positive, negative, or zero—is a simple yet important programming exercise. One key detail that is often overlooked is how to handle zero. Since zero is neither positive nor negative, it should usually be counted separately. This guide explains the standard approach, the Integer.signum() method, and a Java Streams solution.
Problem Statement
Given an array like:
{5, -3, 0, 8, -1, 0, 4}
Count the number of:
- Positive numbers:
5, 8, 4→ 3 - Negative numbers:
-3, -1→ 2 - Zero values:
0, 0→ 2
The Zero Classification Question
Mathematically, zero is neither positive nor negative.
Many beginner programs accidentally include zero in either the positive or negative count, which can produce incorrect results in applications such as financial calculations or statistical analysis.
Unless your requirements explicitly state otherwise, treat zero as a separate category.
Method 1: Classic Loop (Recommended)
public class CountPositiveNegative {
public static void main(String[] args) {
int[] numbers = {5, -3, 0, 8, -1, 0, 4};
int positiveCount = 0;
int negativeCount = 0;
int zeroCount = 0;
for (int num : numbers) {
if (num > 0) {
positiveCount++;
} else if (num < 0) {
negativeCount++;
} else {
zeroCount++;
}
}
System.out.println("Positive: " + positiveCount);
System.out.println("Negative: " + negativeCount);
System.out.println("Zero: " + zeroCount);
}
}
This is the simplest and most efficient solution because it processes the array only once.
Method 2: Using Integer.signum()
for (int num : numbers) {
int sign = Integer.signum(num);
if (sign > 0) {
positiveCount++;
} else if (sign < 0) {
negativeCount++;
} else {
zeroCount++;
}
}
Integer.signum() returns:
1for positive numbers-1for negative numbers0for zero
This makes the code slightly more expressive because it focuses directly on the sign rather than the numeric value.
Method 3: Java Streams
import java.util.Arrays;
long positiveCount =
Arrays.stream(numbers)
.filter(n -> n > 0)
.count();
long negativeCount =
Arrays.stream(numbers)
.filter(n -> n < 0)
.count();
long zeroCount =
Arrays.stream(numbers)
.filter(n -> n == 0)
.count();
This solution is concise but traverses the array three separate times.
Step-by-Step Explanation
Three-Way Classification
Each element is examined using three mutually exclusive conditions.
num > 0→ Positivenum < 0→ Negative- Otherwise → Zero
Since every integer must satisfy exactly one of these conditions, every element is counted exactly once.
Why Use if-else if-else?
Using an if-else if-else chain ensures each element belongs to only one category.
This avoids accidental double-counting that could occur with multiple independent if statements.
Internal Working (Memory View)
For the array:
numbers = [5, -3, 0, 8, -1, 0, 4]
Processing:
5 > 0
Positive = 1
-3 < 0
Negative = 1
0
Zero = 1
8 > 0
Positive = 2
-1 < 0
Negative = 2
0
Zero = 2
4 > 0
Positive = 3
Final counts:
Positive = 3
Negative = 2
Zero = 2
Real-Life Analogy
Imagine organizing financial transactions into three folders.
- Deposits go into the Positive folder.
- Withdrawals go into the Negative folder.
- Transactions with no financial impact go into the Zero folder.
Keeping zero separate avoids ambiguity and maintains accurate records.
Best Practices
- Treat zero as a separate category unless your application specifies otherwise.
- Use an
if-else if-elsechain for mutually exclusive classification. - Consider
Integer.signum()when your code primarily deals with the sign of numbers. - For very large arrays, prefer a single loop over multiple stream operations.
Common Mistakes
- Including zero in either the positive or negative count.
- Using
>= 0instead of> 0, which incorrectly classifies zero as positive. - Traversing the array multiple times when one pass is sufficient.
- Confusing positive with non-negative, since non-negative includes zero.
Expert Tips
Integer.signum()is a useful utility method that makes sign-based logic more readable.- This three-category classification pattern can be reused for problems such as grading systems or temperature classification.
- If you perform this operation frequently, consider returning a small object (or record) containing the three counts instead of managing multiple variables.
Comparison Table
| Method | Passes Through Array | Handles Zero Explicitly | Readability |
|---|---|---|---|
Classic if-else if-else Loop |
1 | Yes | High |
Integer.signum() |
1 | Yes | High |
Streams (filter().count()) |
3 | Yes | Medium |
Frequently Asked Questions
Is zero considered positive or negative?
No. Zero is neither positive nor negative and should generally be counted separately.
What does Integer.signum() return?
It returns:
1for positive numbers-1for negative numbers0for zero
Why should I use if-else if-else instead of separate if statements?
It guarantees that every number belongs to exactly one category, preventing incorrect counting.
Is a single loop more efficient than three stream operations?
Yes. A single loop traverses the array once, whereas three stream filters traverse it three separate times.
What is the difference between positive and non-negative?
A positive number is greater than zero.
A non-negative number is either zero or positive.
Can I use the ternary operator for this problem?
For two categories, yes. For three categories (positive, negative, and zero), an if-else if-else chain is clearer and easier to read.
How can I extend this solution to a 2D array?
Use nested loops to process every row and column while applying the same three-way classification.
Does the same approach work for float and double arrays?
Yes. The same comparisons (>, <, and ==) work, although floating-point values close to zero may require tolerance-based comparisons because of precision limitations.