Whenever we talk about Java 8, what comes in mind most often is ‘Lambda‘ and ‘Streams‘. Am I wrong? I don’t think so. Although these are the most prominent features that came with Java 8, there are other key things that came along too. So, we are going to talk about these less prominent but powerful and important features for any Java developer. These features are nothing but the changes that have been made in the ‘Math‘ class to support ‘Exact Arithmetic‘.

Java 8 introduced new methods in the java.lang.Math class to handle overflows and throws ArithmeticException. These methods are: 

    1. addExact
    2. substractExact
    3. multiplyExact
    4. incrementExact
    5. decrementExact
    6. negateExact

All these methods take either int & long as arguments.

Java 8 Arithmetic Exact Methods...!!! Share on X

Let’s discuss first what these methods are:

1. addExact Adding two arguments using the exact method but will throw an arithmetic exception if an overflow occurs. 

Ex 1.a
System.out.println(Math.addExact(100,500));

Output:
600

Ex 1.b
System.out.println(Math.addExact(100,Integer.MAX_VALUE));

Output:
throws ArithmeticException

2. subtractExact Subtracting the second argument from the first one but will throw an arithmetic Exception if an overflow occurs. 

Ex 2.a
System.out.println(Math.subtractExact(100,1));

Output:
99

Ex 2.b
System.out.println(Math.subtractExact(Integer.MIN_VALUE,1));

Output:
throws ArithmeticException

3. incrementExact Incrementing the argument by one but will throw an arithmetic exception if an overflow occurs. 

Ex 3.a
System.out.println(Math.incrementExact(500));

Output:
501

Ex 3.b
System.out.println(Math.incrementExact(Long.MAX_VALUE));

Output:
throws ArithmeticException

4. decrementExact Decrementing the argument by one but will throw an arithmetic exception if an overflow occurs. 

Ex 4.a
System.out.println(Math.decrementExact(500));

Output:
499

Ex 4.b
System.out.println(Math.decrementExact(Integer.MIN_VALUE));

Output:
throws ArithmeticException

5. multiplyExact Multiply the first and second argument but will throw an arithmetic exception if an overflow occurs. 

Ex 5.a
System.out.println(Math.multiplyExact(10,50));

Output:
500

Ex 5.b
System.out.println(Math.multiplyExact(Integer.MAX_VALUE,50));

Output:
throws ArithmeticException

6. negateExact Alter the sign of the argument but will throw an arithmetic exception if an overflow occurs. 

Ex 6.a
System.out.println(Math.negateExact(500));

Output:
-500

Ex 6.b
System.out.println(Math.negateExact(Long.MIN_VALUE));

Output:
throws ArithmeticException

Example:

public class Math_JAVA8_Exact_Methods {

    public static void main(String[] args) {

        //Addition
        System.out.println(Math.addExact(100, Integer.MAX_VALUE));

        //Subtraction
        System.out.println(Math.subtractExact(Integer.MIN_VALUE, 1));

        //Increment
        System.out.println(Math.incrementExact(Long.MAX_VALUE));

        //Decrement
        System.out.println(Math.decrementExact(Integer.MIN_VALUE));

        //Multiplication
        System.out.println(Math.multiplyExact(Integer.MAX_VALUE, 50));

        //Negate
        System.out.println(Math.negateExact(Long.MIN_VALUE));

    }

}

That’s it. Java 8 Math Methods gives so much convenience to Java developers in order to handle exceptions in such situations that we described above. 

Java 8 Arithmetic Exact Methods...!!! Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts