Java Math addExact() Method

The Math.addExact() method in Java is used to return the sum of two arguments, throwing an ArithmeticException if the result overflows the range of the type.

Table of Contents

  1. Introduction
  2. addExact() Method Syntax
  3. Overloaded addExact() Methods
  4. Examples
    • addExact(int x, int y)
    • addExact(long x, long y)
  5. Real-World Use Case
  6. Conclusion

Introduction

The Math.addExact() method provides a way to perform addition with overflow checking. If the sum of the two arguments exceeds the range of the type (int or long), an ArithmeticException is thrown. This method is useful for ensuring the correctness of arithmetic operations, especially in scenarios where overflow can lead to critical errors.

addExact() Method Syntax

The syntax for the addExact() method varies depending on the type of the arguments:

addExact(int x, int y)

public static int addExact(int x, int y)

addExact(long x, long y)

public static long addExact(long x, long y)

Parameters:

  • x: The first operand.
  • y: The second operand.

Returns:

  • The sum of the arguments.

Throws:

  • ArithmeticException if the result overflows the positive range of the type.

Overloaded addExact() Methods

The Math.addExact() method is overloaded to handle different primitive data types: int and long. Each version returns the sum of the two input arguments, throwing an ArithmeticException if the result overflows the positive range of the type.

Examples

addExact(int x, int y)

The addExact(int x, int y) method returns the sum of two int values, throwing an ArithmeticException if the result overflows the positive int range.

Example

public class AddExactIntExample {
    public static void main(String[] args) {
        int x = Integer.MAX_VALUE;
        int y = 1;

        try {
            int result = Math.addExact(x, y);
            System.out.println("Sum of " + x + " and " + y + " is " + result);
        } catch (ArithmeticException e) {
            System.out.println("Overflow occurred while adding " + x + " and " + y);
        }
    }
}

Output:

Overflow occurred while adding 2147483647 and 1

addExact(long x, long y)

The addExact(long x, long y) method returns the sum of two long values, throwing an ArithmeticException if the result overflows the positive long range.

Example

public class AddExactLongExample {
    public static void main(String[] args) {
        long x = Long.MAX_VALUE;
        long y = 1L;

        try {
            long result = Math.addExact(x, y);
            System.out.println("Sum of " + x + " and " + y + " is " + result);
        } catch (ArithmeticException e) {
            System.out.println("Overflow occurred while adding " + x + " and " + y);
        }
    }
}

Output:

Overflow occurred while adding 9223372036854775807 and 1

Real-World Use Case

Financial Calculations

In financial applications, it's crucial to ensure that arithmetic operations do not overflow, as this could lead to incorrect calculations and significant errors. The Math.addExact() method can be used to perform safe addition operations with overflow checking.

Example

public class FinancialCalculation {
    public static void main(String[] args) {
        int[] transactions = {1000, 2000, Integer.MAX_VALUE};

        int total = 0;
        for (int transaction : transactions) {
            try {
                total = Math.addExact(total, transaction);
                System.out.println("Added transaction: " + transaction + ", Total: " + total);
            } catch (ArithmeticException e) {
                System.out.println("Overflow occurred while adding transaction: " + transaction);
            }
        }
    }
}

Output:

Added transaction: 1000, Total: 1000
Added transaction: 2000, Total: 3000
Overflow occurred while adding transaction: 2147483647

Conclusion

The Math.addExact() method in Java provides a way to perform addition with overflow checking, ensuring that the result does not exceed the range of the type. 

Comments