![]() |
VOOZH | about |
The Math.floorDiv() is a built-in math function in Java that returns the largest (closest to negative infinity) integer value that is less than or equal to the algebraic quotient of two numbers. As floorDiv() is static, object creation is not required. The floorDiv() method performs division that rounds towards negative infinity.
public static int floorDiv(int x, int y)
public static long floorDiv(long x, long y)
Parameters:
The method accepts both int and long data types for the parameters.
Return Value: This method returns the largest integer value that is less than or equal to the algebraic quotient of the division of x by y.
Exception: The method throws an ArithmeticException if the divisor is 0.
Example 1: In this example, we will see how Math.floorDiv() works with both positive numbers and how it differs from the standard division operator.
The result is: 5 The result is: 2
Explanation:
Example 2: In this example, we will see what happens when we attempt to divide by zero using Math.floorDiv().
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zeroWe should use Math.floorDiv() when: