int intValue()

优质
小牛编辑
126浏览
2023-12-01

描述 (Description)

java.math.BigInteger.intValue()将此BigInteger转换为int。 此转换类似于从long到int的缩小基元转换。

如果此BigInteger太大而无法放入int,则仅返回低位32位。 此转换可能会丢失有关BigInteger值的总体大小的信息,并返回具有相反符号的结果。

声明 (Declaration)

以下是java.math.BigInteger.intValue()方法的声明。

public int intValue()

指定 (Specified by)

Number intValue。

参数 (Parameters)

NA

返回值 (Return Value)

此方法返回此BigInteger转换为int。

异常 (Exception)

NA

例子 (Example)

以下示例显示了math.BigInteger.intValue()方法的用法。

package cn.xnip;
import java.math.*;
public class BigIntegerDemo {
   public static void main(String[] args) {
      // create 2 BigInteger objects
      BigInteger bi1, bi2;
      // create 2 Integer objects
      Integer i1, i2;
      // assign values to bi1, bi2
      bi1 = new BigInteger("123");
      bi2 = new BigInteger("9888486986");
      // assign the integer values of bi1, bi2 to i1, i2
      i1 = bi1.intValue();
      i2 = bi2.intValue();
      String str1 = "Integer value of " +bi1+ " is " +i1;
      String str2 = "Integer value of " +bi2+ " is " +i2;
      // print i1, i2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

Integer value of 123 is 123
Integer value of 9888486986 is 1298552394