Java 8中引入了java.math.BigInteger.shortValueExact()。这是一个内置函数,可将BigInteger的值转换为short值并检查丢失的信息。如果BigInteger的值大于32,767或小于-32,768;该方法将抛出ArithmeticException,因为BigInteger不能在短范围内使用。
用法:
public short shortValueExact()
返回值:此方法返回此BigInteger的short值。
异常:如果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:演示shortValueExact()方法用于负数> -32,768
// 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:演示shortValueExact()方法用于负数<-32,768。它将引发算术异常
// 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://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#shortValueExact-
相关用法
- Java BigDecimal shortValueExact()用法及代码示例
- Java BigInteger gcd()用法及代码示例
- Java BigInteger add()用法及代码示例
- Java BigInteger isProbablePrime()用法及代码示例
- Java BigInteger multiply()用法及代码示例
- Java BigInteger subtract()用法及代码示例
- Java 8 BigInteger longValueExact()用法及代码示例
- Java BigInteger nextProbablePrime()用法及代码示例
- Java BigInteger intValueExact()用法及代码示例
- Java BigInteger divide()用法及代码示例
- Java 8 BigInteger byteValueExact()用法及代码示例
- Java 8 BigInteger divideAndRemainder()用法及代码示例
- Java BigInteger sqrtAndRemainder()用法及代码示例
- Java BigInteger and()用法及代码示例
- Java BigInteger pow()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Java 8 | BigInteger shortValueExact() Method with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。