Java-math-biginteger-intvalue

提供:Dev Guides
移動先:案内検索

Java.math.BigInteger.intValue()メソッド

説明

  • java.math.BigInteger.intValue()*は、このBigIntegerをintに変換します。 この変換は、longからintへのプリミティブ変換の縮小に類似しています。

このBigIntegerが大きすぎてintに収まらない場合、下位32ビットのみが返されます。 この変換は、BigInteger値の全体的な大きさに関する情報を失い、逆符号の結果を返す可能性があります。

宣言

次に、* java.math.BigInteger.intValue()*メソッドの宣言を示します。

public int intValue()

指定者

クラス Number のintValue。

パラメーター

NA

戻り値

このメソッドは、このBigIntegerをintに変換して返します。

例外

NA

次の例は、math.BigInteger.intValue()メソッドの使用法を示しています。

package com.finddevguides;

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