Java.math.BigInteger.intValue() Method


Advertisements


Description

The java.math.BigInteger.intValue() converts this BigInteger to an int. This conversion is analogous to a narrowing primitive conversion from long to int.

If this BigInteger is too big to fit in an int, only the low-order 32 bits are returned. This conversion can lose information about the overall magnitude of the BigInteger value as well as return a result with the opposite sign.

Declaration

Following is the declaration for java.math.BigInteger.intValue() method.

public int intValue()

Specified by

intValue in class Number.

Parameters

NA

Return Value

This method returns this BigInteger converted to an int.

Exception

NA

Example

The following example shows the usage of math.BigInteger.intValue() method.

package com.tutorialspoint;

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 );
   }
}

Let us compile and run the above program, this will produce the following result −

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

java_math_biginteger.htm

Advertisements