toIntExact() in Java

toIntExact(): This method is available in the Math class of Java.

Syntax:

int java.lang.Math.toIntExact(long value)

This method takes one argument of type long as its parameter. This method returns the value of the long argument, throwing an exception if the value overflows an int. 

Parameters: One parameter is required for this method.

value: the long value.

Returns: the argument as an int.

Throws: ArithmeticException - if the argument overflows an int.

For Example:

Math.toIntExact(18088) = > It returns  18088.

Approach

Java

public class TointExact {
    public static void main(String[] args) {

        long value = 18088;
        System.out.println(Math.toIntExact(value));

    }
}

Output:

18088

No comments:

Post a Comment