Floor division is a regular division operation, except it yields the
Integer.MIN_VALUE
and -1
, respectively, then the division operation results in integer overflow. Hence, the result will be Integer.MIN_VALUE
.floorDiv
and the normal division operation are the same.floorDiv
returns the integer less than or equal to the quotient, and normal division returns the integer closest to zero.The fractional representation would be .
The normal division operation results in as the quotient.
The floor division operation results in as the quotient.
The fractional representation would be .
The normal division operation results in as the quotient.
The floor division operation results in , as the quotient because the signs of the dividend and divisor are different.
The floorDiv
method is introduced in Java 8.
floorDiv
is defined in the Math
class, which is defined in the java.lang
package.
To import the Math
class, use the import statement below:
import java.lang.Math;
public static int floorDiv(int x, int y)
int x
: dividendint y
: divisorThe floorDiv
method returns the largest value that is less than or equal to the algebraic quotient.
public static long floorDiv(long x, int y)
public static long floorDiv(long x, long y)
import java.lang.Math;public class Main{public static void main(String[] args){int dividend = 23;int divisor = -4;int quotient = Math.floorDiv(dividend, divisor);System.out.printf("floorDiv(%d, %d) = %d", dividend, divisor, quotient);System.out.println();dividend = 23;divisor = 4;quotient = Math.floorDiv(dividend, divisor);System.out.printf("floorDiv(%d, %d) = %d", dividend, divisor, quotient);}}