Integer signum() Method in Java
Last Updated :
19 Aug, 2021
The signum function is an odd mathematical function that extracts the sign of a real number. The signum function is also known as sign function.
The Integer.signum() method of java.lang returns the signum function of the specified integer value. For a positive value, a negative value and zero the method returns 1, -1 and 0 respectively.
Syntax :
public static int signum(int a)
Parameter: The method takes one parameter a of integer type on which the operation is to be performed.
Return Value: The method returns the signum function of the specified integer value. If the specified value is negative it returns -1, 0 if the specified value is zero, and 1 if the specified value is positive.
Examples:
Consider an integer a = 27
Since 27 is a positive int signum will return= 1
Consider another integer a = -61
Since -61 is a negative int signum will return= -1
Consider the integer value a = 0
For a zero signum, it will return = 0
Below programs illustrate the Java.lang.Integer.signum() Method:
Program 1:
Java
// Java program to illustrate the
// Java.lang.Integer.signum() Method
import java.lang.*;
public class Geeks {
public static void main(String[] args) {
// It will return 1 as the int value is greater than 1
System.out.println(Integer.signum(1726));
// It will return -1 as the int value is less than 1
System.out.println(Integer.signum(-13));
// It will returns 0 as int value is equal to 0
System.out.println(Integer.signum(0));
}
}
Program 2:
Java
// Java program to illustrate the
// Java.lang.Integer.signum() Method
import java.lang.*;
public class Geeks {
public static void main(String[] args) {
// It will return 1 as the int value is greater than 1
System.out.println(Integer.signum(-1726));
// It will return -1 as the int value is less than 1
System.out.println(Integer.signum(13));
// It will returns 0 as int value is equal to 0
System.out.println(Integer.signum(0));
}
}
Program 3: For a decimal value and string.
Note: It returns an error message when a decimal value and string is passed as an argument.
Java
// Java program to illustrate the
// Java.lang.Integer.signum() Method
import java.lang.*;
public class Geeks {
public static void main(String[] args) {
//returns compile time error as passed argument is a decimal value
System.out.println(Integer.signum(3.81));
//returns compile time error as passed argument is a string
System.out.println(Integer.signum("77"));
}
}
Output: prog.java:10: error: incompatible types: possible lossy conversion from double to int
System.out.println(Integer.signum(3.81));
^
prog.java:14: error: incompatible types: String cannot be converted to int
System.out.println(Integer.signum("77"));
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
2 errors