Java 8 | BigInteger shortValueExact() Method with Examples
Last Updated :
04 Dec, 2018
Improve
java.math.BigInteger.shortValueExact() was introduced in Java 8. It is an inbuilt function which converts the value of BigInteger to a short and checks for lost information. If the value of BigInteger is greater than 32,767 or less than -32,768; the method will throw ArithmeticException as BigInteger doesn’t fit in short range.
Syntax:
Java
Java
Java
Java
public short shortValueExact()Return Value: This method returns the short value of this BigInteger. Exception: The method throws ArithmeticException if the value of BigInteger is greater than 32,767 or less than -32,768; as this range doesn’t fit in short range. Example:
Input: 15,564 Output: 15,564 Explanation: 15,564 is given as input which is BigInteger and short value of 15,564 is 15,564 Input: -17,584 Output: -17,584 Explanation: -17,584 is given as input which is BigInteger and short value of -17,584 is -17,584 Input: 40000 Output: ArithmeticException Explanation: When 40000 is tried to convert to short, since 40000 > 32,767 (greater than a short's range), therefore it throws an arithmetic exception.Below programs illustrates shortValueExact() method of BigInteger class: Program 1: To demonstrate shortValueExact() method for positive number <32,767
// Java program to demonstrate shortValueExact()
// method of BigInteger Class
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigInteger object
BigInteger big;
big = new BigInteger("15564");
// print value of BigInteger
System.out.println("BigInteger value : "
+ big);
// convert big to the short value
short b1 = big.shortValueExact();
// print short value
System.out.println("short converted value : "
+ b1);
}
}
Output:
Program 2: To demonstrate shortValueExact() method for negative number >-32,768
BigInteger value : 15564 short converted value : 15564
// Java program to demonstrate shortValueExact()
// method of BigInteger Class
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigInteger object
BigInteger big = new BigInteger("-17584");
// print value of BigInteger
System.out.println("BigInteger value : "
+ big);
// convert big to the short value
short b1 = big.shortValueExact();
// print short value
System.out.println("short converted value : "
+ b1);
}
}
Output:
Program 3: To demonstrate shortValueExact() method for negative number <-32,768. It will throw Arithmetic Exception
BigInteger value : -17584 short converted value : -17584
// Java program to demonstrate shortValueExact()
// method of BigInteger Class
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigInteger object
BigInteger big = new BigInteger("-40000");
// print value of BigInteger
System.out.println("BigInteger value : "
+ big);
try {
// convert big to the short value
short b1 = big.shortValueExact();
// print short value
System.out.println("short converted value : "
+ b1);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Output:
Program 4: To demonstrate shortValueExact() method for positive number >32,767. It will throw Arithmetic Exception
BigInteger value : -40000 Exception: java.lang.ArithmeticException: BigInteger out of short range
// Java program to demonstrate shortValueExact()
// method of BigInteger Class
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigInteger object
BigInteger big = new BigInteger("40000");
// print value of BigInteger
System.out.println("BigInteger value : "
+ big);
try {
// convert big to the short value
short b1 = big.shortValueExact();
// print short value
System.out.println("short converted value : "
+ b1);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Output:
Reference: https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#shortValueExact--
BigInteger value : 40000 Exception: java.lang.ArithmeticException: BigInteger out of short range