Signup/Sign In
LAST UPDATED: OCTOBER 16, 2020

Java Integer signum() Method

Java signum() is a part of the Integer class of the java.lang package. This method is used to return the signum function value of the value passed.

The signum function is the function that extracts the sign of the real number. Hence, the signum function value returns :

1; if the value is positive

0; if the value is zero

-1; if the value is negative

Syntax:

public static int signum(int i)

Parameters:

The integer value of which the signum value is to be returned.

Returns:

Returns the signum value of the integer value passed as a parameter. The value will be 1, 0 or -1 for the positive, zero, and negative numbers respectively.

Example 1:

Here, the signum function value for a positive, negative, and a zero is shown:

import java.lang.Integer;

public class StudyTonight
{  
    public static void main(String[] args) 
    {  
      int a = 9;
      int b = -4;
      int c = 0;
      //signum value for positive number  
      System.out.println("Signum value of " + a + " is " +Integer.signum(a)); 
      //signum value for negative number
      System.out.println("Signum value of " + b + " is " +Integer.signum(b)); 
      // signum value for zero
      System.out.println("Signum value of " + c + " is " +Integer.signum(c)); 
    }  
}  


Signum value of 9 is 1
Signum value of -4 is -1
Signum value of 0 is 0

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.util.Scanner;  

public class StudyTonight
{  
    public static void main(String[] args)
    {  
        try
        {
           System.out.print("Enter Value :");       
           Scanner sc = new Scanner(System.in);  
           int a = sc.nextInt();  
           int res = Integer.signum(a);  //return the signum value
           System.out.print("Signum value is "+res);
        }
        catch(Exception e)
        {
          System.out.println("Invalid Input!!");
        }   
    }  
}  


Enter Value : 65
Signum value is 1
**************************
Enter Value : -34
Signum value is -1
***************************
Enter Value : 0
Signum value is 0

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.