devxlogo

Understandng toExactInt method in java.lang.Math package

Understandng toExactInt method in java.lang.Math package

java.lang.Math has numerous methods and our interest here is toIntExact() method.
Consider the following example

public class MathExact
{
   public static void main(String args[])
   {
      MathExact mathExact = new MathExact();
      mathExact.proceed();
   }
   
   private void proceed()
   {
      long l = 100000000;
      int i = (int) l;

      System.out.println(“i: ” + i);
      
      System.out.println(“Math.toIntExact(“+l+”);: ” + Math.toIntExact(l));
   }
}

/*

Expected output:

[root@mypc]# java MathExact
i: 100000000
Math.toIntExact(100000000);: 100000000

*/

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist