Java 8 | BigInteger shortValueExact() Method with Examples
Last Updated :
04 Dec, 2018
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--
Similar Reads
BigInteger subtract() Method in Java with Examples The java.math.BigInteger.subtract(BigInteger val) is used to calculate the Arithmetic difference of two BigIntegers. This method is applicable on large value numbers of range much greater than the range of biggest data type double of Java without compromising with the precision of the result but as
3 min read
Byte shortValue() method in Java with examples The shortValue() method of Byte class is a built in method in Java which is used to return the value of this Byte object as short. Syntax ByteObject.shortValue() Return Value: It returns the value of ByteObject as short. Below is the implementation of shortValue() method in Java: Example 1: Java //
2 min read
Java 8 | BigInteger longValueExact() Method with Examples java.math.BigInteger.longValueExact() was introduced in Java 8. It is an inbuilt function which converts the value of BigInteger to a long and checks for lost information. If the value of BigInteger is greater than 9,223,372,036,854,775,807 or less than -9,223,372,036,854,775,808; the method will th
3 min read
Double shortValue() method in Java with examples The shortValue() method of Double class is a built in method to return the value specified by the calling object as short after type casting.Syntax: DoubleObject.shortValue() Return Value: It return the value of DoubleObject as short.Below programs illustrate shortValue() method in Java: Program 1:
2 min read
BigInteger add() Method in Java with Examples The java.math.BigInteger.add(BigInteger val) is used to calculate the Arithmetic sum of two BigIntegers. This method is used to find arithmetic addition of large numbers of range much greater than the range of biggest data type double of java without compromising with the precision of the result. Th
3 min read