Signup/Sign In

Java Long signum() Method

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

A signum function is a function that extracts the sign of the real number.

Syntax:

public static int signum(long i)

Parameters:

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

Returns:

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

Example 1:

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

import java.lang.Long;

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


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);  
           long a = sc.nextLong();  
           long res = Long.signum(a);  //return the signum value
           System.out.print("Signum value is "+res);
        }
        catch(Exception e)
        {
           System.out.println("Invalid Input!!");
        } 
    }  
}


Enter Value : 8556
Signum value is 1
***************************
Enter Value : -56
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.