BigInteger intValueExact() Method in Java with Examples
Last Updated :
04 Dec, 2018
java.math.BigInteger.intValueExact() was introduced in Java 8. It is an inbuilt function which converts the value of BigInteger to a int and checks for lost information. If the value of BigInteger is greater than 2,147,483,647 or less than -2,147,483,648; the method will throw
ArithmeticException as BigInteger doesn’t fit in int range.
Syntax:
public int intValueExact()
Return Value: This method returns the int value of this BigInteger.
Exception: The method throws
ArithmeticException if the value of BigInteger is greater than 2,147,483,647 or less than -2,147,483,648; as this range doesn’t fit in int range.
Example:
Input: 4561561
Output: 4561561
Explanation: 4561561 is given as input which is bigInteger
and int value of 4561561 is 4561561
Input: -8546512
Output: -8546512
Explanation: -8546512 is given as input which is bigInteger
and int value of -8546512 is -8546512
Input: 3000000000
Output: ArithmeticException
Explanation: When 3000000000 is tried to convert to int,
since 3000000000 > 2,147,483,647 (greater than a int's range),
therefore it throws an arithmetic exception.
Below programs illustrates intValueExact() method of BigInteger class:
Program 1: To demonstrate intValueExact() method for positive number <2,147,483,647
Java
// Java program to demonstrate intValueExact()
// method of BigInteger Class
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigInteger object
BigInteger big;
big = new BigInteger("4561561");
// print value of BigInteger
System.out.println("BigInteger value : "
+ big);
// convert big to the int value
int b1 = big.intValueExact();
// print int value
System.out.println("int converted value : "
+ b1);
}
}
Output:
BigInteger value : 4561561
int converted value : 4561561
Program 2: To demonstrate intValueExact() method for negative number >-2,147,483,648
Java
// Java program to demonstrate intValueExact()
// method of BigInteger Class
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigInteger object
BigInteger big = new BigInteger("-8546512");
// print value of BigInteger
System.out.println("BigInteger value : "
+ big);
// convert big to the int value
int b1 = big.intValueExact();
// print int value
System.out.println("int converted value : "
+ b1);
}
}
Output:
BigInteger value : -8546512
int converted value : -8546512
Program 3: To demonstrate intValueExact() method for negative number <-2,147,483,648. It will throw Arithmetic Exception
Java
// Java program to demonstrate intValueExact()
// method of BigInteger Class
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigInteger object
BigInteger big = new BigInteger("-3000000000");
// print value of BigInteger
System.out.println("BigInteger value : "
+ big);
try {
// convert big to the int value
int b1 = big.intValueExact();
// print int value
System.out.println("int converted value : "
+ b1);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Output:
BigInteger value : -3000000000
Exception: java.lang.ArithmeticException: BigInteger out of int range
Program 4: To demonstrate intValueExact() method for positive number >2,147,483,647. It will throw Arithmetic Exception
Java
// Java program to demonstrate intValueExact()
// method of BigInteger Class
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigInteger object
BigInteger big = new BigInteger("3000000000");
// print value of BigInteger
System.out.println("BigInteger value : "
+ big);
try {
// convert big to the int value
int b1 = big.intValueExact();
// print int value
System.out.println("int converted value : "
+ b1);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Output:
BigInteger value : 3000000000
Exception: java.lang.ArithmeticException: BigInteger out of int range
Reference: https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#intValueExact--
Similar Reads
AtomicInteger intValue() method in Java with examples The java.util.concurrent.atomic.AtomicInteger.intValue() is an inbuilt method in java that returns the value which is currently stored in the object which is of data-type int. Syntax: public int intValue() Parameters: The function does not accepts a single parameter. Return value: The function retur
1 min read
Byte intValue() method in Java with examples The intValue() method of Byte class is a built in method in Java which is used to return the value of this Byte object as int. Syntax ByteObject.intValue() Return Value: It returns the value of ByteObject as int. Below is the implementation of intValue() method in Java: Example 1: Java // Java code
2 min read
Java 8 | BigInteger longValueExact() Method with Examples java.math.BigInteger.longValueExact() was introduced in Java 8. It is an inbuilt function which converts the value of BigInteger to a long and checks for lost information. If the value of BigInteger is greater than 9,223,372,036,854,775,807 or less than -9,223,372,036,854,775,808; the method will th
3 min read
DoubleAdder intValue() method in Java with Examples The java.DoubleAdder.intValue() is an inbuilt method in java that returns the sum() as an int after a narrowing primitive conversion. When the object of the class is created its initial value is zero. Syntax: public int intValue() Parameters: This method does not accepts any parameter. Return Value:
1 min read
Double intValue() method in Java with examples The intValue() method of Double class is a built in method to return the value specified by the calling object as int after type casting. Syntax: DoubleObject.intValue() Return Value: It return the value of DoubleObject as int. Below programs illustrate intValue() method in Java: Program 1: Java //
2 min read