BigInteger shortValueExact() in Java

shortValueExact(): This method is available in java.math.BigInteger class of Java.

Syntax:

short java.math.BigInteger.shortValueExact()

This method converts this BigInteger to a short, checking for lost information. If the value of this BigIntegeris out of the range of the short type, then an ArithmeticException is thrown.

Returns: this BigInteger converted to a short.

Throws: ArithmeticException - if the value of this will not exactly fit in a short.

For Example:

BigInteger bigInteger = new BigInteger("1234")

bigInteger.shortValueExact() = > It returns 1234.

Approach

Java

import java.math.BigInteger;

public class BigIntegershortValueExact {
    public static void main(String[] args) {

        BigInteger bigInteger = new BigInteger("1234");
        System.out.println(bigInteger.shortValueExact());
    }
}

Output:

1234

No comments:

Post a Comment