Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

biginteger
java
communitycreator

What is the BigInteger.signum method 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.

In Java, the BigInteger class handles calculations with very big integral values that are beyond the limits of the primitive integer type.

The signum method of the BigInteger class retrieves the sign of the specified big integer.

Syntax

public int signum()

Parameter

This method does not take any arguments.

Return value

This method returns:

  • 1 if the big integer calling the method is greater than zero.

  • -1 if the big integer calling the method is less than zero.

  • 0 if the big integer calling the method is equal to zero.

Code

The example below demonstrates how to use the signum method.

import java.math.BigInteger;
class BigIntegerRemainderExample {
public static void main( String args[] ) {
BigInteger val1 = new BigInteger("10");
BigInteger val2 = new BigInteger("-10");
BigInteger val3 = new BigInteger("0");
System.out.println("Signum of " + val1 + " :"+ val1.signum());
System.out.println("Signum of " + val2 + " : "+ val2.signum());
System.out.println("Signum of " + val3 + " : "+ val3.signum());
}
}

Explanation

In the above code:

  • In line number 1: We have imported the BigInteger class.
import java.math.BigInteger;
  • In lines number 5, 6, and 7: We have created three BigInteger objects, val1 with a value of 10, val2 with a value of -10, and val3 with a value of0.
BigInteger val1 = new BigInteger("10");
BigInteger val2 = new BigInteger("-10");
BigInteger val3 = new BigInteger("0");
  • In line number 8: We have called the signum method on the created BigInteger objects.
    • For the val1 object, we get 1 as the result because val1 > 0.
    • For the val2 object, we get -1 as the result because val2 < 0.
    • For the val3 object, we get 0 as the result because val3 = 0.
val1.signum(); //  1
val2.signum(); // -1
val3.signum(); //  0

RELATED TAGS

biginteger
java
communitycreator

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