Java – Find Sum of Int – addExact() Method – Examples & Explanation

java.lang.Math.addExact() method is used to find the sum of a int value for the given input (x – parameter) in Java.
throwing an exception if the result overflows an int.

Package

addExact() method exists in Math class of java.lang package.
Since: JDK1.8

 

Declaration of addExact() Method

The declaration of addExact() method is:

public static int addExact(int x, int y)

 

Syntax of addExact() Method

The syntax of addExact() method in Java is:

 
java.lang.Math.addExact(int x, int y)

Example

/*
*Find the sum of given input(int) without importing the 
java.lang Package.
*/
public class JavaMathExample {
    public static void main(String[] args) {
        //Declaring variables
        int x = -4876.1874;
        int result;
        //Assign sum of x to result variable
        result = java.lang.Math.addExact(x);
        //Printing result to the console
        System.out.println(result);
    }
}

Output:

Example
By importing java.lang package the syntax of addExact() method can be used as:

/*
*Find the sum of given input(int) by importing the java.lang Package.
*/
import java.lang.*;  //importing package

public class JavaMathExample {
    public static void main(String[] args) {
        
        //Declaring variables
        int x = -68.97;
        int result;
        
        //Assign sum of x to result variable
        result = Math.addExact(x);
        
        //Printing result to the console
        System.out.println(result);
    }
}

Output:

 

Special Cases of addExact() Method in Java

ArithmeticException – if the result overflows an int
Example

/*
*This example shows some special cases of addExact() method in Java
*/
import java.lang.*; //importing java.lang package

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

        //Declaring variables
        int positiveZero = 0;
        int negativeZero = -0;
        int positiveInfinity = Int.POSITIVE_INFINITY;
        int negativeInfinity = Int.NEGATIVE_INFINITY;
        int nan = Double.NaN;
        int result;

        //Here argument is positive zero, output will also be positive zero
        result = Math.addExact(positiveZero);
        System.out.println(result);

        //Here argument is negative zero, output will be positive zero
        result = Math.addExact(negativeZero);
        System.out.println(result);

        //Here argument is positive infinity, output will also be positive infinity
        result = Math.addExact(positiveInfinity);
        System.out.println(result);

        //Here argument is negative infinity, output will be negative infinity
        result = Math.addExact(negativeInfinity);
        System.out.println(result);

        //Here argument is NaN, output will be NaN
        result = Math.addExact(nan);
        System.out.println(result);


    }

}

Output:

 

Parameters of addExact() Method in Java

x – Where x is any valid int input value. This parameter is required.

 

Error Handling

If the x parameter is not a number addExact() method returns an error.
If there is no argument (x – input value) passes to the method, the compiler will produce an error.
Example

/*
*This example returns a compile time error
*/
import java.lang.*; //importing java.lang package

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

        //Declaring variables
        String x = "Not a number";
        int result;

        //argument is not a number
        result = Math.addExact(x);
        System.out.println(result);

        //Here no argument is used
        result = Math.addExact();
        System.out.println(result);


    }

}

Output:
Error

 

Return Value of addExact() Method in Java

addExact() method returns the sum of a int value for the given input (x – parameter).

 

Java addExact() Method Example 1

import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {
        int negativeValue = -18.869;
        int positiveValue = 27.78639;
        int zero = 0;
        System.out.println("sum of a Negative Value: "+Math.addExact(negativeValue));
        System.out.println("sum of a Positive Value: "+Math.addExact(positiveValue));
        System.out.println("sum of Zero: "+Math.addExact(zero));

    }
}

Output:

 

Java addExact() Method Example 2

import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {
        //Declare and initialize an array containing int values
        int test [] = { Int.MAX_VALUE, Int.MIN_VALUE, 
		Int.NaN, Int.MIN_EXPONENT, 42.867e-12, 16.852, 0, -2.58, -78.8569e7 };

        //Use for loop to print sum of each element of the array
        for (int i=0; i < test.length; i++) {
            System.out.println(  Math.addExact(test[i])  );
        }

    }
}

Output: