Integer signum() Method in Java
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 opearation 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 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)); } } |
1 -1 0
Program 2:
// 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)); } } |
-1 1 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 program to illustrate the // Java.lang.Integer.signum() Method import java.lang.*; public class Geeks { public static void main(String[] args) { //retuns compile time error as passed argument is a decimal value System.out.println(Integer.signum(3.81)); //retuns compile time error as passed argument is a string System.out.println(Integer.signum("77")); } } |
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
Recommended Posts:
- BigInteger signum() Method in Java
- Java signum() method with Examples
- Long signum() Method in Java
- BigDecimal signum() Method in Java
- StrictMath signum() Method in Java
- Integer sum() Method in Java
- Integer rotateLeft() Method in Java
- Integer decode() Method in Java
- Integer floatValue() Method in Java
- Integer toOctalString() Method in Java
- Integer lowestOneBit() Method in Java
- Integer highestOneBit() Method in Java
- Java Integer compareUnsigned() method
- Integer valueOf() Method in Java
- Java Integer compareTo() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



