BigInteger intValue() Method in Java

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

intValue of 32145 : 32145
intValue of 7613721 : 7613721

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

intValue of 4326516236135 : 1484169063
intValue of 251362466336 : -2040604128

Reference:
BigInteger intValue() Docs



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.