Java Integer.signum() – Examples
In this tutorial, we will learn about Java Integer.signum() method, and learn how to use this method to find the value of signum function for a given integer, with the help of examples.
signum(int i)
Integer.signum(i) returns the signum function of the specified int value i.
Syntax
The syntax of signum() method with integer value as parameter is
Integer.signum(int i)
where
| Parameter | Description |
|---|---|
| i | The integer value whose signum has to be found. |
Returns
The method returns value of type int.
Example 1 – signum(i) – i is Positive
In this example, we will take a positive integer and find the signum function of this integer using Integer.signum() method. Since we passed a positive integer as argument, signum() returns 1.
Java Program
public class Example {
public static void main(String[] args){
int i = 4;
int result = Integer.signum(i);
System.out.println("Result of signum("+i+") = " + result);
}
}
Output
Result of signum(4) = 1
Example 2 – signum(i) – i is Zero
In this example, we will find the signum function of zero using Integer.signum() method. signum() should return zero for returns for input zero.
Java Program
public class Example {
public static void main(String[] args){
int i = 0;
int result = Integer.signum(i);
System.out.println("Result of signum("+i+") = " + result);
}
}
Output
Result of signum(0) = 0
Example 3 – signum(i) – i is Negative
In this example, we will take a negative integer and find the signum function of this integer using Integer.signum() method. Since we passed a negative integer as argument, signum() returns -1.
Java Program
public class Example {
public static void main(String[] args){
int i = -5;
int result = Integer.signum(i);
System.out.println("Result of signum("+i+") = " + result);
}
}
Output
Result of signum(-5) = -1
Conclusion
In this Java Tutorial, we have learnt the syntax of Java Integer.signum() method, and also how to use this method with the help of Java example programs.
