Guayaba de Java | Método IntMath.checkedSubtract(int a, int b) con ejemplos

ElcheckSubtract (int a, int b) es un método de IntMath Class de Guava que acepta dos parámetros a y b , y devuelve su diferencia.

Sintaxis:

public static int checkedSubtract(int a, int b)

Parámetros: El método acepta dos valores int a y b y calcula su diferencia.

Valor devuelto: el método devuelve la diferencia de los valores int que se le pasan, siempre que no se desborde.

Excepciones: el métodocheckedSubtract(int a, int b) lanza ArithmeticException si la diferencia, es decir, (a – b) se desborda en la aritmética con signo int.

Los siguientes ejemplos ilustran la implementación del método anterior:

Ejemplo 1:

// 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);
    }
}
Producción:

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

Ejemplo 2:

// 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);
        }
    }
}
Producción:

java.lang.ArithmeticException: overflow

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

Publicación traducida automáticamente

Artículo escrito por Sahil_Bansall y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *