StrictMath.floorDiv(int, int) in Java

StrictMath.floorDiv(int, int): This method is available in java.lang.StrictMath class of Java.

Syntax:

int java.lang.StrictMath.floorDiv(int x, int y)

This method takes two arguments of type int as its parameters. This method returns the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient.

Note:

If the dividend is the Integer.MIN_VALUE and the divisor is -1, then integer overflow occurs and the result is equal to the Integer.MIN_VALUE.

Parameters: Two parameters are required for this method.

x: the dividend.

y: the divisor.

Returns: the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient.

Throws:

ArithmeticException - if the divisor y is zero.

Approach 1: When no exceptions.

Java

public class StrictMathfloorDivint {
    public static void main(String[] args) {

        int x = 145, y = 4;
        System.out.println(StrictMath.floorDiv(x, y));
    }
}

Output:

36


Approach 2: ArithmeticException

Java

public class StrictMathfloorDivint {
    public static void main(String[] args) {

        int x = 145, y = 0;
        System.out.println(StrictMath.floorDiv(x, y));
    }
}


Output:

Exception in thread "main" java.lang.ArithmeticException: / by zero at java.base/java.lang.Math.floorDiv(Math.java:1170) at java.base/java.lang.StrictMath.floorDiv(StrictMath.java:989)


No comments:

Post a Comment