StrictMath.floorDiv(long, int) in Java

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

Syntax:

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

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

Note:

If the dividend is Long.MIN_VALUE and the divisor is -1, then integer overflow occurs and the result is equal to Long.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 StrictMathfloorDivlongint {
    public static void main(String[] args) {

        long x = 199191;
        int y = 19;
        System.out.println(StrictMath.floorDiv(x, y));
    }
}

Output:

10483


Approach 2: ArithmeticException

Java

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

        long x = 199191;
        int 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:1233) at java.base/java.lang.Math.floorDiv(Math.java:1204) at java.base/java.lang.StrictMath.floorDiv(StrictMath.java:1013)


No comments:

Post a Comment