BigInteger intValue() Method in Java
Last Updated :
07 Oct, 2022
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 lose 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
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
BigInteger b1, b2;
b1 = new BigInteger( "32145" );
b2 = new BigInteger( "7613721" );
int intValueOfb1 = b1.intValue();
int intValueOfb2 = b2.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
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
BigInteger b1, b2;
b1 = new BigInteger( "4326516236135" );
b2 = new BigInteger( "251362466336" );
int intValueOfb1 = b1.intValue();
int intValueOfb2 = b2.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
Share your thoughts in the comments
Please Login to comment...