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
import java.math.*;
public class GFG {
public static void main(String[] args)
{
BigInteger big;
big = new BigInteger( "15564" );
System.out.println( "BigInteger value : "
+ big);
short b1 = big.shortValueExact();
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
import java.math.*;
public class GFG {
public static void main(String[] args)
{
BigInteger big = new BigInteger( "-17584" );
System.out.println( "BigInteger value : "
+ big);
short b1 = big.shortValueExact();
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
import java.math.*;
public class GFG {
public static void main(String[] args)
{
BigInteger big = new BigInteger( "-40000" );
System.out.println( "BigInteger value : "
+ big);
try {
short b1 = big.shortValueExact();
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
import java.math.*;
public class GFG {
public static void main(String[] args)
{
BigInteger big = new BigInteger( "40000" );
System.out.println( "BigInteger value : "
+ big);
try {
short b1 = big.shortValueExact();
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–
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
04 Dec, 2018
Like Article
Save Article