Related Articles

Related Articles

Integer signum() Method in Java
  • Last Updated : 05 Dec, 2018

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
}

chevron_right


Output:

1
-1
0

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
}

chevron_right


Output:

-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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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"));
  
  
}

chevron_right


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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :