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

subtractExact()

Example

Perform integer subtraction using the subtractExact() method.

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

Definition and Usage

The subtractExact() method calculates the difference between two integers, triggering an exception if the operation results in overflow. This safeguard ensures accurate results, particularly when subtracting extremely large negative numbers.

Syntax

One of the following:

public static int subtractExact(int x, int y)

 

public static long subtractExact(long x, long y)

Parameter Values

Parameter

Description

x

“Necessary. The number from which subtraction is performed.”

y

“Necessary. The value to be subtracted.”

Technical Details

Returns:

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

Throws:

ArithmeticException – If the subtraction operation results in an overflow.

Java Version:

1.8+