StrictMath toIntExact() Method in Java with Examples

The java.lang.StrictMath.toIntExact() is an inbuilt method in Java is used to return the value of the long argument. If the result overflows an int it will throw an exception. Object creation is not mandatory as toIntExact(long num) is static.

Syntax:

public static int toIntExact(long num)

Parameters: The method accepts one parameter num of long type whose int value is returned.



Return Value: The method returns the argument as an int.

Exception: If the argument overflows an int it throws ArithmeticException.

Examples:

Input: num = 2727l
Output: 2727

Input: num = -86262l
Output: -86262

Below programs illustrate the java.lang.StrictMath.toIntExact() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working
// of java.lang.StrictMath.toIntExact() method
  
import java.lang.StrictMath;
  
class Geeks {
  
    // driver code
    public static void main(String args[])
    {
  
        // Get the long value
        // whose IntExact value is needed
        long num = 266526l;
  
        // Get the IntExact value
        // using toIntExact() method
        int intvalue = StrictMath.toIntExact(num);
  
        // Print the IntExact value
        System.out.print("IntExact value of "
                         + num + " = " + intvalue);
    }
}

chevron_right


Output:

IntExact value of 266526 = 266526

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working
// of java.lang.StrictMath.toIntExact() method
  
import java.lang.StrictMath;
  
class Geeks {
  
    // driver code
    public static void main(String args[])
    {
  
        // Get the long value
        // whose IntExact value is needed
        long num = -7226526l;
  
        // Get the IntExact value
        // using toIntExact() method
        int intvalue = StrictMath.toIntExact(num);
  
        // Print the IntExact value
        System.out.print("IntExact value of "
                         + num + " = " + intvalue);
    }
}

chevron_right


Output:

IntExact value of -7226526 = -7226526

Program 3: To demonstrate ArithmeticException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working
// of java.lang.StrictMath.toIntExact() method
  
import java.lang.StrictMath;
  
class Geeks {
  
    // driver code
    public static void main(String args[])
    {
  
        try {
            // Get the long value
            // whose IntExact value is needed
            long num = 654456645546l;
  
            System.out.println("Trying to get "
                               + "IntExact value of: "
                               + num);
  
            // Get the IntExact value
            // using toIntExact() method
            int intvalue = StrictMath.toIntExact(num);
  
            // Print the IntExact value
            System.out.print("IntExact value of "
                             + num + " = " + intvalue);
        }
        catch (Exception e) {
            System.out.println("Exception throwm: " + e);
        }
    }
}

chevron_right


Output:

Trying to get IntExact value of: 654456645546
Exception throwm: java.lang.ArithmeticException: integer overflow


My Personal Notes arrow_drop_up

lets make it a lil simple

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.