Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

subtractexact
math
java
communitycreator

What is Math.subtractExact() in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The subtractExact method takes two arguments and returns the difference between them. The arguments will be either int or long.

subtractExact is a staticAny static member can be accessed before any objects of its class are created and without reference to any object. method present in the Math class.

Syntax


public static long subtractExact(long a, long b);

public static int subtractExact(int a, int b);

Arguments

  • a: First value (int/long).

  • b: Second value, which will be subtracted from first value (int/long).

Return value

This method returns the difference between a and b.

The subtractExact method is equivalent to:

argument1 - argument2

Code

Example 1

import java.lang.Math;
class SubtractExactTest {
public static void main(String cmdArgs[]) {
int a = 20;
int b = 10;
int diff = Math.subtractExact(a, b);
System.out.print("The difference of "+ a + " and " + b + " is ");
System.out.println(diff);
long la = 100l;
long lb = 50l;
long ldiff = Math.subtractExact(la, lb);
System.out.print("The difference of "+ la + " and " + lb + " is ");
System.out.println(ldiff);
}
}

Explanation 1

In the code above:

  • We created four variables, a and b, of the int and la, and lb of the long type.

  • We called the Math.subtractExact method with a and b as arguments. This will return the difference of a and b as result.

  • Again, we called the Math.subtractExact method with la and lb as arguments. This will return the sum of la and lb as a result.


The subtractExact method will throw ArithmeticException if:

  • the arguments are int and the difference overflows the int value.

  • the arguments are long and the difference overflows the long value.

Example 2

Consider if the two arguments are int. The ArithmeticException will be thrown if the difference of the two int arguments is less than the minimum value or greater than the maximum value that the int type can hold.

import java.lang.Math;
class SubtractExactTest {
public static void main(String cmdArgs[]) {
try{
int a = Integer.MIN_VALUE;
int b = 1;
int sum = Math.subtractExact(a, b);
}catch(Exception e){
System.out.println(e);
}
}
}

Explanation 2

In the code above, we have created two variables, a and b, of the int type. We assigned the minimum value an integer can hold to the variable a, and 1 to the variable b.

When we call the subtractExact method with a and b as arguments, we will get ArithmeticException because the difference of a and b is less than the value an integer can hold.

RELATED TAGS

subtractexact
math
java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring