Java Tutorial Java Advanced Java References

Java Math - floorDiv() Method



The Java floorDiv() method returns the largest integer value or largest long value that is less than or equal to the algebraic quotient. The method first calculates the quotient by dividing the first argument with the second argument and finally returns the floor() of the quotient. The method can be overloaded and it can take int and long arguments. In special cases it returns the following:

  • If the second argument is zero, this method throws ArithmeticException.

Syntax

public static int floorDiv(int x, int y)
public static long floorDiv(long x, long y)

Parameters

x Specify the dividend.
y Specify the divisor.

Return Value

Returns the largest integer value or largest long value that is less than or equal to the algebraic quotient.

Exception

Throws ArithmeticException, if the divisor y is zero.

Example:

In the example below, floorDiv() method returns the floor() of algebraic quotient.

public class MyClass {
 public static void main(String[] args) {
  System.out.println(Math.floorDiv(11, 4));   
  System.out.println(Math.floorDiv(10, 7));
  System.out.println(Math.floorDiv(3, -1));     
 }
}

The output of the above code will be:

2
1
-3

❮ Java Math Methods