Introduction
After learning how to check even or odd numbers, the next logical building block in Java is classifying a number as positive, negative, or zero. Although the program is simple, it introduces one of the most common decision-making structures in programming—the if-else ladder.
This type of conditional branching appears everywhere in real-world applications, from grading systems and pricing calculations to authentication and business rules. Understanding how it works builds a strong foundation for writing more complex decision-making logic.
In this tutorial, you'll learn multiple ways to determine whether a number is positive, negative, or zero in Java, understand how the JVM evaluates these conditions internally, and explore common mistakes, best practices, and interview tips.
Understanding Positive, Negative, and Zero
In mathematics:
-
A number greater than 0 is positive.
-
A number less than 0 is negative.
-
0 is neither positive nor negative.
Java expresses these rules using relational operators:
-
>(greater than) -
<(less than) -
==(equal to)
The goal is to ensure that exactly one condition executes for every possible input.
Method 1: Using an If-Else Ladder
The if-else ladder is the most common and beginner-friendly solution.
Java Program
public class Main {
public static void main(String[] args) {
int num = -10;
if (num > 0) {
System.out.println(num + " is a positive number.");
} else if (num < 0) {
System.out.println(num + " is a negative number.");
} else {
System.out.println("The number is zero.");
}
}
}
Output
-10 is a negative number.
Time Complexity
O(1)
Space Complexity
O(1)
Method 2: Using the Ternary Operator
The ternary operator provides a shorter version of the same logic.
Java Program
public class Main {
public static void main(String[] args) {
int num = -10;
String result =
(num > 0)
? "positive"
: (num < 0)
? "negative"
: "zero";
System.out.println("The number is " + result + ".");
}
}
Output
The number is negative.
Although this solution is compact, nested ternary operators can reduce readability if overused.
Time Complexity
O(1)
Space Complexity
O(1)
Method 3: Taking User Input Using Scanner
Instead of hardcoding the number, you can accept input from the user.
Java Program
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (num > 0) {
System.out.println(num + " is positive.");
} else if (num < 0) {
System.out.println(num + " is negative.");
} else {
System.out.println("The number is zero.");
}
scanner.close();
}
}
Sample Output
Enter a number: 15
15 is positive.
Time Complexity
O(1)
Space Complexity
O(1)
Method 4: Using Integer.signum()
Java provides the Integer.signum() method, which returns:
-
1for positive numbers -
0for zero -
-1for negative numbers
Java Program
public class Main {
public static void main(String[] args) {
int num = -10;
int sign = Integer.signum(num);
switch (sign) {
case 1 ->
System.out.println(num + " is positive.");
case -1 ->
System.out.println(num + " is negative.");
default ->
System.out.println("The number is zero.");
}
}
}
Output
-10 is negative.
This approach uses Java's standard library and works especially well when processing large collections of numbers.
Time Complexity
O(1)
Space Complexity
O(1)
How Java Handles This Internally
Consider:
int num = -10;
Internally:
-
numis stored as a primitive integer in the stack memory. -
The JVM compares
numwith0using relational operators. -
Conditions are evaluated from top to bottom.
-
As soon as one condition evaluates to
true, the remaining conditions are skipped. -
After the
main()method finishes, the stack frame is destroyed automatically.
Except for the Scanner object in Method 3, none of these approaches create objects on the heap.
Real-Life Analogy
Think about checking the outside temperature.
-
Above 0°C means the temperature is positive.
-
Below 0°C means the temperature is negative.
-
Exactly 0°C represents the freezing point.
Your program performs the same comparison by checking whether the number is above, below, or equal to zero.
Comparison of Different Methods
| Method | Readability | Best Use Case |
|---|---|---|
| If-Else Ladder | ⭐⭐⭐⭐⭐ | Recommended for most programs |
| Ternary Operator | ⭐⭐⭐⭐ | Short, compact expressions |
| Scanner | ⭐⭐⭐⭐⭐ | Interactive console applications |
| Integer.signum() | ⭐⭐⭐⭐ | Batch processing and switch-based logic |
Best Practices
-
Prefer the if-else ladder for maximum readability.
-
Avoid deeply nested ternary operators.
-
Validate user input before processing.
-
Close the
Scannerobject after reading input. -
Use descriptive variable names such as
numorinputValue.
Common Mistakes
Using >= Instead of >
Incorrect:
if (num >= 0)
This incorrectly classifies zero as positive.
Correct:
if (num > 0)
Forgetting to Handle Zero
Incorrect:
if (num > 0) {
}
else if (num < 0) {
}
Zero is never handled.
Always include:
else {
System.out.println("The number is zero.");
}
Comparing Floating-Point Values with Exact Zero
When working with double or float, exact equality comparisons can sometimes be unreliable because of floating-point precision.
Forgetting to Close Scanner
Always call:
scanner.close();
after reading user input.
Expert Tips
-
Use the if-else ladder as the standard interview solution.
-
Mention
Integer.signum()as an alternative to demonstrate familiarity with the Java standard library. -
Explain that Java evaluates conditions from top to bottom and stops once a condition becomes true.
-
Remember that all approaches execute in constant time.
Pros and Cons
| Method | Advantages | Disadvantages |
|---|---|---|
| If-Else Ladder | Readable, easy to maintain | Slightly more verbose |
| Ternary Operator | Compact and concise | Less readable when nested |
| Integer.signum() | Elegant and reusable | Less familiar to beginners |
Frequently Asked Questions
How do I check whether a number is positive, negative, or zero?
Use an if-else ladder:
if (num > 0)
for positive,
else if (num < 0)
for negative,
and
else
for zero.
Is zero positive or negative?
No.
Zero is neither positive nor negative.
Can I use a ternary operator?
Yes.
A nested ternary operator can classify positive, negative, and zero in a single expression.
What does Integer.signum() return?
-
1→ Positive -
0→ Zero -
-1→ Negative
Does this logic work for decimal numbers?
Yes.
The same relational operators work for double and float, although floating-point precision should be considered when comparing exact values.
How can I take user input?
Use the Scanner class:
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
What is the time complexity?
Every solution performs only a fixed number of comparisons.
Time Complexity:
O(1)
Why does the order of conditions matter?
Java evaluates conditions sequentially and stops after the first condition that evaluates to true.
Can I use a switch statement directly?
Not with relational comparisons.
However, combining Integer.signum() with a switch statement is a clean alternative.
What happens if I omit the final else block?
When the number is zero, no branch executes, resulting in incorrect program behavior.
Is this question commonly asked in interviews?
Yes.
It is a popular beginner-level interview question because it tests your understanding of relational operators and conditional branching.
How is this different from checking even or odd?
Even/odd checking uses the modulus operator (%) to test divisibility by 2.
Positive/negative/zero checking uses relational operators (>, <, and ==) to compare the value directly against zero.