Home Java Integer Methods signum() Java Integer signum() Method Definition In Java, Integer.signum(i) tells you the sign of an int. It returns -1 if the number is negative, 0 if it is zero, and 1 if it is positive.
The result is always one of those three values, so it works nicely in a switch statement, or as a step direction that moves a number toward a target.
A static method of java.lang.Integer (since Java 1.5): call it on the class, as Integer.signum(...). Integer lives in java.lang, so no import is needed.
Syntax One argument: the number to examine.
int a = Integer.signum(-42); // -1
int b = Integer.signum(0); // 0
int c = Integer.signum(42); // 1 Call What it does Integer.signum(i)-1, 0, or 1 for negative, zero, or positive. Integer.signum(a - b)Direction from b to a (careful: subtraction can overflow). Integer.compare(a, b)The safe way to compare two numbers instead. i == 0 ? 0 : (i > 0 ? 1 : -1)What signum does, written out by hand.
The result is never anything other than -1, 0, or 1, even for Integer.MIN_VALUE and Integer.MAX_VALUE.
Library declarations // java.lang.Integer
public static int signum(int i) Parameters Parameter Description i (int)The number whose sign you want.
Return Value Return Value Description -1i is negative.0i is zero.1i is positive.
Important
signum never throws and never overflows, and it is the one place where an int has exactly three possible answers. Be careful when you pass a subtraction such as signum(a - b): if the subtraction overflows, the sign comes out wrong. Use Integer.compare(a, b) to compare two values safely.
Compare with Similar Methods Related methods:
Java Examples 1. The three answers 2. Choose a message 3. Move toward a target Example 1 — See the Sign of Several Numbers Print the sign of a negative number, zero, a positive number, and the extremes of int.
public class IntegerSignumDemo {
public static void main(String[] args) {
int[] numbers = {-42, -1, 0, 1, 42, Integer.MIN_VALUE, Integer.MAX_VALUE};
for (int n : numbers) {
System.out.println("signum(" + n + ") = " + Integer.signum(n));
}
}
} signum(-42) = -1
signum(-1) = -1
signum(0) = 0
signum(1) = 1
signum(42) = 1
signum(-2147483648) = -1
signum(2147483647) = 1 How It Works Negative gives -1. Both -42 and -1 return -1, whatever their size.Zero gives 0. Zero is neither negative nor positive, so it has its own answer.Positive gives 1. Both 1 and 42 return 1.The extremes too. MIN_VALUE returns -1 and MAX_VALUE returns 1; the size of the number never matters.Example 2 — Describe Temperatures with a switch Use the three possible answers of signum as the cases of a switch statement.
public class IntegerSignumSwitch {
static String describe(int celsius) {
switch (Integer.signum(celsius)) {
case -1:
return celsius + " C: below freezing";
case 0:
return celsius + " C: exactly freezing";
default:
return celsius + " C: above freezing";
}
}
public static void main(String[] args) {
int[] readings = {-8, 0, 17};
for (int reading : readings) {
System.out.println(describe(reading));
}
}
} -8 C: below freezing
0 C: exactly freezing
17 C: above freezing How It Works A switch on three values. Because signum returns only -1, 0, or 1, the switch covers every case.case -1. Any negative temperature, such as -8, lands here and is below freezing.case 0. Only 0 itself matches, so it is described as exactly freezing.The default case. The only value left is 1, so a positive reading like 17 is above freezing.Example 3 — Walk a Number Toward a Target Use the sign of the remaining distance as a step, so the same code works in both directions.
public class IntegerSignumStep {
static void walk(int start, int target) {
int position = start;
StringBuilder path = new StringBuilder(String.valueOf(position));
while (position != target) {
position += Integer.signum(target - position);
path.append(" -> ").append(position);
}
System.out.println(path);
}
public static void main(String[] args) {
walk(2, -2);
walk(-1, 3);
walk(4, 4);
}
} 2 -> 1 -> 0 -> -1 -> -2
-1 -> 0 -> 1 -> 2 -> 3
4 How It Works The sign is the direction. target - position is negative when the target is behind and positive when it is ahead, so signum gives a step of -1 or 1.Walking down. From 2 to -2, each step adds -1: 2, 1, 0, -1, -2.Walking up. From -1 to 3, each step adds 1 and the same code works unchanged.Already there. When start and target are equal, the loop never runs and only the starting number is printed.Frequently Asked Questions What does Integer.signum return? It returns -1 for a negative int, 0 for zero, and 1 for a positive int.
How do I check whether an int is negative, zero, or positive? Call Integer.signum(i), or compare directly with i < 0, i == 0, and i > 0. Both are correct; signum is handy in a switch.
Does signum work for very large numbers? Yes. Integer.MIN_VALUE returns -1 and Integer.MAX_VALUE returns 1.
Is signum(a - b) a safe way to compare two numbers? Not always. The subtraction can overflow for large values. Use Integer.compare(a, b) instead.
What about signum for double values? Math.signum works on double and float and returns a floating-point result. Integer.signum is only for int.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion