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.

Syntax:

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:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation of
// checkedSubtract(int a, int b) method
// of Guava's IntMath class
  
import java.math.RoundingMode;
import com.google.common.math.IntMath;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
        int a1 = 25;
        int b1 = 36;
  
        // Using checkedSubtract(int a, int b)
        // method of Guava's IntMath class
        int ans1 = IntMath.checkedSubtract(a1, b1);
  
        System.out.println("Difference of " + a1 + " and "
                           + b1 + " is: " + ans1);
  
        int a2 = 150;
        int b2 = 667;
  
        // Using checkedSubtract(int a, int b)
        // method of Guava's IntMath class
        int ans2 = IntMath.checkedSubtract(a2, b2);
  
        System.out.println("Difference of " + a2 + " and "
                           + b2 + " is: " + ans2);
    }
}

chevron_right


Output:

Difference of 25 and 36 is: -11
Difference of 150 and 667 is: -517

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation of
// checkedSubtract(int a, int b) method
// of Guava's IntMath class
  
import java.math.RoundingMode;
import com.google.common.math.IntMath;
  
class GFG {
  
    static int findDiff(int a, int b)
    {
        try {
  
            // Using checkedSubtract(int a, int b) method
            // of Guava's IntMath class
            // This should throw "ArithmeticException"
            // as the difference overflows in signed
            // int arithmetic
            int ans = IntMath.checkedSubtract(a, b);
  
            // Return the answer
            return ans;
        }
        catch (Exception e) {
            System.out.println(e);
            return -1;
        }
    }
  
    // Driver code
    public static void main(String args[])
    {
        int a = Integer.MIN_VALUE;
        int b = 452;
  
        try {
  
            // Function calling
            findDiff(a, b);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

chevron_right


Output:

java.lang.ArithmeticException: overflow

Reference: https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/IntMath.html#checkedSubtract-int-int-



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.