Related Articles

Related Articles

Java Guava | Longs.checkedSubtract(long a, long b) method with Examples
  • Last Updated : 31 Jan, 2019

The checkedSubtract(long a, long b) is a method of Guava’s LongMath Class which accepts two parameters a and b, and returns their difference.

Syntax:

public static long checkedSubtract(long a, long b)

Parameters: The method accepts two long values a and b and computes their difference.

Return Value: The method returns the difference of long values passed to it, provided it does not overflow.

Exceptions: The method checkedSubtract(long a, long b) throws ArithmeticException if the difference i.e, (a – b) overflows in signed long 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(long a, long b) method
// of Guava's LongMath class
  
import java.math.RoundingMode;
import com.google.common.math.LongMath;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
        long a1 = 25;
        long b1 = 36;
  
        // Using checkedSubtract(long a, long b)
        // method of Guava's LongMath class
        long ans1 = LongMath.checkedSubtract(a1, b1);
  
        System.out.println("Difference of " + a1 + " and "
                           + b1 + " is: " + ans1);
  
        long a2 = 150;
        long b2 = 667;
  
        // Using checkedSubtract(long a, long b)
        // method of Guava's LongMath class
        long ans2 = LongMath.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(long a, long b) method
// of Guava's LongMath class
  
import java.math.RoundingMode;
import com.google.common.math.LongMath;
  
class GFG {
  
    static long findDiff(long a, long b)
    {
        try {
  
            // Using checkedSubtract(long a, long b) method
            // of Guava's LongMath class
            // This should throw "ArithmeticException"
            // as the difference overflows in signed
            // long arithmetic
            long ans = LongMath.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[])
    {
        long a = Long.MIN_VALUE;
        long 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/LongMath.html#checkedSubtract-long-long-

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :