Open In App

Java 8 | BigInteger shortValueExact() Method with Examples

Last Updated : 04 Dec, 2018
Suggest changes
Share
Like Article
Like
Report
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:
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
// 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:
 BigInteger value : 15564 short converted value : 15564 
Program 2: To demonstrate shortValueExact() method for negative number >-32,768 Java
// 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:
 BigInteger value : -17584 short converted value : -17584 
Program 3: To demonstrate shortValueExact() method for negative number <-32,768. It will throw Arithmetic Exception Java
// 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:
 BigInteger value : -40000 Exception: java.lang.ArithmeticException: BigInteger out of short range 
Program 4: To demonstrate shortValueExact() method for positive number >32,767. It will throw Arithmetic Exception Java
// 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:
 BigInteger value : 40000 Exception: java.lang.ArithmeticException: BigInteger out of short range 
Reference: https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#shortValueExact--

Explore