Java 8中引入了java.math.BigInteger.intValueExact()。它是一个内置函数,它将BigInteger的值转换为int并检查丢失的信息。如果BigInteger的值大于2,147,483,647或小于-2,147,483,648;该方法将抛出ArithmeticException,因为BigInteger不在int范围内。
用法:
public int intValueExact()
返回值:此方法返回此BigInteger的int值。
异常:如果BigInteger的值大于2,147,483,647或小于-2,147,483,648,则该方法引发ArithmeticException。因为此范围不适合int范围。
例:
Input: 4561561 Output: 4561561 Explanation: 4561561 is given as input which is bigInteger and int value of 4561561 is 4561561 Input: -8546512 Output: -8546512 Explanation: -8546512 is given as input which is bigInteger and int value of -8546512 is -8546512 Input: 3000000000 Output: ArithmeticException Explanation: When 3000000000 is tried to convert to int, since 3000000000 > 2,147,483,647 (greater than a int's range), therefore it throws an arithmetic exception.
以下示例程序旨在说明BigInteger类的intValueExact()方法:
示例1:演示intValueExact()方法为正数
// Java program to demonstrate intValueExact()
// 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("4561561");
// print value of BigInteger
System.out.println("BigInteger value : "
+ big);
// convert big to the int value
int b1 = big.intValueExact();
// print int value
System.out.println("int converted value : "
+ b1);
}
}
输出:
BigInteger value : 4561561 int converted value : 4561561
示例2:演示用于负数> -2,147,483,648的intValueExact()方法
// Java program to demonstrate intValueExact()
// method of BigInteger Class
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigInteger object
BigInteger big = new BigInteger("-8546512");
// print value of BigInteger
System.out.println("BigInteger value : "
+ big);
// convert big to the int value
int b1 = big.intValueExact();
// print int value
System.out.println("int converted value : "
+ b1);
}
}
输出:
BigInteger value : -8546512 int converted value : -8546512
示例3:演示intValueExact()方法的负数
// Java program to demonstrate intValueExact()
// method of BigInteger Class
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigInteger object
BigInteger big = new BigInteger("-3000000000");
// print value of BigInteger
System.out.println("BigInteger value : "
+ big);
try {
// convert big to the int value
int b1 = big.intValueExact();
// print int value
System.out.println("int converted value : "
+ b1);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
输出:
BigInteger value : -3000000000 Exception: java.lang.ArithmeticException: BigInteger out of int range
示例4:演示intValueExact()方法用于正数> 2,147,483,647。它将引发算术异常
// Java program to demonstrate intValueExact()
// method of BigInteger Class
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigInteger object
BigInteger big = new BigInteger("3000000000");
// print value of BigInteger
System.out.println("BigInteger value : "
+ big);
try {
// convert big to the int value
int b1 = big.intValueExact();
// print int value
System.out.println("int converted value : "
+ b1);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
输出:
BigInteger value : 3000000000 Exception: java.lang.ArithmeticException: BigInteger out of int range
参考: https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#intValueExact–
相关用法
- Java BigDecimal intvalueExact()用法及代码示例
- Java BigInteger add()用法及代码示例
- Java BigInteger gcd()用法及代码示例
- Java 8 BigInteger byteValueExact()用法及代码示例
- Java 8 BigInteger divideAndRemainder()用法及代码示例
- Java BigInteger sqrtAndRemainder()用法及代码示例
- Java BigInteger nextProbablePrime()用法及代码示例
- Java BigInteger multiply()用法及代码示例
- Java BigInteger divide()用法及代码示例
- Java 8 BigInteger longValueExact()用法及代码示例
- Java BigInteger subtract()用法及代码示例
- Java BigInteger isProbablePrime()用法及代码示例
- Java 8 BigInteger shortValueExact()用法及代码示例
- Java BigInteger abs()用法及代码示例
- Java BigInteger or()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 BigInteger intValueExact() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。