BigInteger intValueExact() Method in Java with Examples (original) (raw)

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

}

`