dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Tip: Understandng the toExactInt Method in the java.lang.Math Package

Explore the toIntExact() method in the java.lang.Math Package.


advertisement

The java.lang.Math has numerous methods and our interest here is to explore the 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

*/

 

Visit the DevX Tip Bank

 



   
Sridhar M S. is a Java developer from Bangalore, India. He holds a master's degree in Computer Science.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date