Sunday, December 7, 2025
HomeLanguagesJavaJava Math toIntExact(long value) Method

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 returns the input argument as an int(integer) .
Exception :
It throws ArithmeticException - if the result overflows an int

Example :To show working of java.lang.Math.toIntExact() method.




// Java program to demonstrate working
// of java.lang.Math.toIntExact() method
import java.lang.Math;
  
class Gfg1 {
    // driver code
    public static void main(String args[])
    {
        long a = 499;
        System.out.println(Math.toIntExact(a));
    }
}


Output:

499




// Java program to demonstrate working
// of java.lang.Math.toIntExact() method
import java.lang.Math;
  
class Gfg2 {
  
    // driver code
    public static void main(String args[])
    {
        long x = Long.MAX_VALUE;
  
        System.out.println(Math.toIntExact(x));
    }
}


Output:

Runtime Error :
Exception in thread "main" java.lang.ArithmeticException: integer overflow
    at java.lang.Math.toIntExact(Math.java:1011)
    at Gfg2.main(File.java:12)
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32427 POSTS0 COMMENTS
Milvus
103 POSTS0 COMMENTS
Nango Kala
6803 POSTS0 COMMENTS
Nicole Veronica
11944 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12014 POSTS0 COMMENTS
Shaida Kate Naidoo
6934 POSTS0 COMMENTS
Ted Musemwa
7188 POSTS0 COMMENTS
Thapelo Manthata
6882 POSTS0 COMMENTS
Umr Jansen
6867 POSTS0 COMMENTS