Java 8 | BigInteger shortValueExact() Method with Examples

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

BigInteger value : 15564
short converted value : 15564

Program 2: To demonstrate shortValueExact() method for negative number >-32,768

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

BigInteger value : -17584
short converted value : -17584

Program 3: To demonstrate shortValueExact() method for negative number <-32,768. It will throw Arithmetic Exception

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


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–



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.