Description

On this document we will be showing a java example on how to use the signum() method of BigInteger Class. Basically this method returns the signum function of this BigInteger.

Method Syntax

public int signum()

Method Argument

Data Type Parameter Description
N/A N/A N/A

Method Returns

The abs() method returns -1, 0 or 1 as the value of this BigInteger is negative, zero or positive.

Compatibility

Requires Java 1.1 and up

Java BigInteger signum() Example

Below is a java code demonstrates the use of signum() method of BigInteger class. The example presented might be simple however it shows the behavior of the signum() method.

package com.javatutorialhq.java.examples;

import java.math.BigInteger;
import java.util.Scanner;

/*
 * A java example source code to demonstrate
 * the use of signum() method of BigInteger class
 */

public class BigIntegerSignumExample {

	public static void main(String[] args) {	
		
		// get user input		 
		System.out.print("Enter a value:");
		Scanner s = new Scanner(System.in);
		String input = s.nextLine();
		s.close();		
		
		// convert string input to BigInteger
		BigInteger val1 = new BigInteger(input);
		// get the signum of the input
		System.out.println("signum = "+val1.signum());		
	}

}

This example is a lot simpler than it looks. Basically we ask for user input and converted this value into BigInteger. The result of the signum() method has been printed at the end of the code.

Sample Output

Below is the sample output when you run the above example.

BigInteger signum() example output