Open In App

StrictMath toIntExact() Method in Java with Examples

Last Updated : 29 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:




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


Output:

IntExact value of 266526 = 266526

Program 2:




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


Output:

IntExact value of -7226526 = -7226526

Program 3: To demonstrate ArithmeticException




// 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 thrown: " + e);
        }
    }
}


Output:

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


Previous Article
Next Article

Similar Reads

Java Math toIntExact(long value) Method
The java.lang.Math.toIntExact() is a built-in math function in java which returns the value of the long argument.It throws an exception if the result overflows an int.As toIntExact(long value) is static, so object creation is not required. Syntax : public static int toIntExact(long value) Parameter : value : the long value Return : This method retu
1 min read
StrictMath asin() Method in Java With Examples
The java.lang.StrictMath.asin() is an inbuilt method of StrictMath class which is used to return the arc sine of a specified value. The returned angle is in within the range of -pi/2 and pi/2. The method gives rise to two special results: If the absolute value of the argument is greater than 1 or the argument is itself a NaN then the result is also
2 min read
StrictMath ceil() Method in Java with Examples
All methods of java.lang.StrictMath class : Set 1, Set 2The java.lang.StrictMath.ceil() is an inbuilt method in Java is used to return the smallest double value, greater than or equal to the given double argument and equal to an integer. It gives rise to three special results: The result is the same as the argument when the given argument is equal
2 min read
StrictMath cos() Method in Java with Examples
All methods of java.lang.StrictMath class : Set 1, Set 2The java.lang.StrictMath.cos() is an inbuilt method in Java which is used to return the cosine value of a given angle. The method returns NaN when the given argument is NaN or infinity.Syntax: public static double cos(double num) Parameters: The method accepts one parameter num of double type
2 min read
StrictMath expm1() Method in Java With Examples
The java.lang.StrictMath.expm1() is an inbuilt method in Java which is used to return the exponential e^num -1 for a given value of num. The method gives rise to four different cases: The method returns NaN when the given argument is NaN.The result is positive infinity when the argument is positive infinity.The result is negative infinity when the
2 min read
StrictMath min() Method in Java with Examples
The java.lang.StrictMath.min() method returns the smaller of two values. There are four variations of this method with different types of parameters passed. All of them are discussed below : min(double num1, double num2) is the inbuilt method of StrictMath class which is used to get the least of given two double values arguments. It returns NaN whe
6 min read
StrictMath max() Method in Java With Examples
The java.lang.StrictMath.max() method returns the larger of two values. There are four variations of this method with different types of parameters passed. All of them are discussed below : The max(double num1, double num2)is the inbuilt method which is used to get the maximum of two given double values arguments. It returns the same value when num
6 min read
StrictMath ulp() Method In Java with Examples
ulp(double num) The ulp(double num) is an inbuilt method of StrictMath class in java which is used to get the size of an ulp of the given argument. An ulp of a double value is the positive distance between this floating point value and the double value which is next larger in magnitude.Some special cases are: The result is NaN, if the argument is N
3 min read
StrictMath fma() method in Java with Examples
In StrictMath class, there are two types of fma() method depending upon the parameters passed to it. The methods are: fma(double a, double b, double c): This method of StrictMath class is used to return the exact product of the first two doubles with the addition of the third double and then round the result to the nearest double. The rounding is d
5 min read
StrictMath multiplyHigh() method in Java with Examples
The multiplyHigh(long x, long y) method of StrictMath class is used to return the most significant 64 bits from the 128-bit product of two 64-bit factors. This method takes two long values as input and a long is a 64-bit number. This method multiplies both long values and returns the most significant 64 bits from the product result which can be 128
2 min read
Article Tags :
Practice Tags :