Skip to content
Related Articles

Related Articles

BigInteger intValue() Method in Java
  • Last Updated : 04 Dec, 2018

The java.math.BigInteger.intValue() converts this BigInteger to an integer value. If the value returned by this function is too big to fit into integer value, then it will return only the low-order 32 bits. Further there is chance that this conversion can loose information about the overall magnitude of the BigInteger value. This method can also return the result with opposite sign.

Syntax:

public int intValue()

Returns: The method returns an int value which represents integer value for this BigInteger.

Examples:

Input: BigInteger1=32145
Output: 32145
Explanation: BigInteger1.intValue()=32145.

Input: BigInteger1=4326516236135
Output: 1484169063
Explanation: BigInteger1.intValue()=1484169063. This BigInteger is too big for 
intValue so it is returning lower 32 bit.

Example 1:Below programs illustrate intValue() method of BigInteger class






// 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);
    }
}
Output:
intValue of 32145 : 32145
intValue of 7613721 : 7613721

Example 2: when return integer is too big for int value.




// 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);
    }
}
Output:
intValue of 4326516236135 : 1484169063
intValue of 251362466336 : -2040604128

Reference:
BigInteger intValue() Docs

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :