Java BigInteger intValueExact()方法及示例

Java BigInteger intValueExact()方法及示例

java.math.BigInteger.intValueExact() 是在Java 8中引入的。它是一个内置的函数,可以将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范围。

例子

输入: 4561561
输出: 4561561
解释: 4561561被作为输入,它是bigInteger
和4561561的int值是4561561

输入: -8546512
输出: -8546512
解释: -8546512被作为输入,它是bigInteger。
和-8546512的int值是-8546512

输入: 3000000000
输出: ArithmeticException
解释: 当3000000000被尝试转换为int时。
因为3000000000>2,147,483,647(大于int的范围)。
因此它抛出了一个算术异常。

以下程序说明了BigInteger类的intValueExact()方法。

程序1: 演示<2,147,483,647的正数的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: 演示负数<-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("-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: 演示正数>2,147,483,647的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

参考资料: https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#intValueExact-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程