Java BigInteger intValue()方法

Java BigInteger intValue()方法

java.math.BigInteger.intValue() 将这个BigInteger转换为一个整数值。如果这个函数返回的值过大,无法装入整数值,那么它将只返回低阶32位。此外,这种转换有可能会丢失BigInteger值的整体大小的信息。这个方法也可以返回相反符号的结果。

语法 :

public int intValue()

返回: 该方法返回一个int值,代表这个BigInteger的整数值。

示例

输入: BigInteger1=32145
输出: 32145
解释: BigInteger1.intValue()=32145.

输入: BigInteger1=4326516236135
输出: 1484169063
解释: BigInteger1.intValue()=1484169063. 这个BigInteger对于
intValue太大了,所以它返回低32位。

例1:以下程序说明了BigInteger类的intValue()方法

// Java program to demonstrate
// intValue() method of BigInteger
 
import java.math.BigInteger;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // Creating 2 BigInteger objects
        BigInteger b1, b2;
 
        b1 = new BigInteger("32145");
        b2 = new BigInteger("7613721");
 
        // apply intValue() method
        int intValueOfb1 = b1.intValue();
        int intValueOfb2 = b2.intValue();
 
        // print intValue
        System.out.println("intValue of "
                           + b1 + " : " + intValueOfb1);
        System.out.println("intValue of "
                           + b2 + " : " + intValueOfb2);
    }
}

输出

intValue of 32145 : 32145
intValue of 7613721 : 7613721

例2:当返回的整数对int值来说太大。

// Java program to demonstrate
// intValue() method of BigInteger
 
import java.math.BigInteger;
 
public class GFG {
 
    public static void main(String[] args)
    {
 
        // Creating 2 BigInteger objects
        BigInteger b1, b2;
 
        b1 = new BigInteger("4326516236135");
        b2 = new BigInteger("251362466336");
 
        // apply intValue() method
        int intValueOfb1 = b1.intValue();
        int intValueOfb2 = b2.intValue();
 
        // print intValue
        System.out.println("intValue of "
                           + b1 + " : " + intValueOfb1);
        System.out.println("intValue of "
                           + b2 + " : " + intValueOfb2);
    }
}

输出

intValue of 4326516236135 : 1484169063
intValue of 251362466336 : -2040604128

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程