Java Guava | IntMath.checkedSubtract(int a, int b) method with Examples
The checkedSubtract(int a, int b) is a method of Guava’s IntMath Class which accepts two parameters a and b, and returns their difference.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
public static int checkedSubtract(int a, int b)
Parameters: The method accepts two int values a and b and computes their difference.
Return Value: The method returns the difference of int values passed to it, provided it does not overflow.
Exceptions: The method checkedSubtract(int a, int b) throws ArithmeticException if the difference i.e, (a – b) overflows in signed int arithmetic.
Below examples illustrate the implementation of above method:
Difference of 25 and 36 is: -11 Difference of 150 and 667 is: -517