📜  Java 8 | BigInteger shortValueExact() 方法和示例

📅  最后修改于: 2022-05-13 01:55:35.143000             🧑  作者: Mango

Java 8 | BigInteger shortValueExact() 方法和示例

Java.math.BigInteger.shortValueExact()是在Java 8 中引入的。它是一个内置函数,可将 BigInteger 的值转换为短值并检查丢失的信息。如果 BigInteger 的值大于 32,767 或小于 -32,768;该方法将抛出ArithmeticException ,因为 BigInteger 不适合短距离。

句法:

public short shortValueExact()

返回值:此方法返回此 BigInteger 的短值。

异常:如果 BigInteger 的值大于 32,767 或小于 -32,768,则该方法抛出ArithmeticException ;因为这个范围不适合短距离。

例子:

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.

下面的程序说明了 BigInteger 类的 shortValueExact() 方法:

程序 1:演示正数 <32,767 的 shortValueExact() 方法

// 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);
    }
}
输出:
BigInteger value : 15564
short converted value : 15564

程序 2:演示负数 >-32,768 的 shortValueExact() 方法

// 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);
    }
}
输出:
BigInteger value : -17584
short converted value : -17584

程序 3:演示负数 <-32,768 的 shortValueExact() 方法。它会抛出算术异常

// 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);
        }
    }
}
输出:
BigInteger value : -40000
Exception: java.lang.ArithmeticException: BigInteger out of short range

程序 4:演示用于正数 >32,767 的 shortValueExact() 方法。它会抛出算术异常

// 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);
        }
    }
}
输出:
BigInteger value : 40000
Exception: java.lang.ArithmeticException: BigInteger out of short range

参考: https: Java/math/BigInteger.html#shortValueExact–