Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

addExact()

Example

Add integers:

System.out.println(Math.addExact(12, 16));
System.out.println(Math.addExact(10000, 24060));

Definition and Usage

The addExact() method adds two integers but raises an exception if the sum exceeds the maximum value that the integer type can hold, ensuring accurate calculations and preventing potential errors resulting from integer overflow.

Syntax

The addExact() method adds two integers, but if the result exceeds the maximum value allowed for the integer type, it triggers an exception, safeguarding against inaccuracies due to overflow.

public static int addExact(int x, int y)
public static long addExact(long x, long y)

Parameter Values

Parameter

Description

x

Necessary. The initial number to be added.

y

Mandatory. The second number to be added.

Technical Details

Returns:

An integer or long value representing the result of adding two numbers together.

Throws:

ArithmeticException occurs if the addition results in an overflow.

Java Version:

1.8+