Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
java

What is the Integer.signum function in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The signum static function of the Integer class is used to get the signum function of a specified integer value.

What is the signum function?

The signum denotes the sign of a real number.

  • A real number less than zero is denoted by -1.
  • A real number greater than zero is denoted by -1.
  • A real number equal to zero is denoted by 0.

Syntax

public static int signum(int i)

Parameters

This method takes the int value as an argument.

Return value

This method returns:

  • 1 if the argument is greater than zero.
  • -1 if the argument is less than zero.
  • 0 if the argument is equal to zero.

Code

The example below uses the signum function as follows:

class IntegerSignumExample {
    public static void main(String[] args) {
        System.out.println("Signum for -10 is " + Integer.signum(-10));
        System.out.println("Signum for 10 is " + Integer.signum(10));
        System.out.println("Signum for 0 is " + Integer.signum(0));
    }
}

In the code above:

  • In line 3 we call Integer.signum(-10l). This returns -1 because -10 is less than zero.

  • In line 4 we call Integer.signum(10). This returns -1 because 10 is greater than zero.

  • In line 5 we call Integer.signum(0). This returns 0 because the argument is equal to zero.

RELATED TAGS

communitycreator
java

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring