Java Math floorDiv() Method
The floorDiv () method of Math class returns the largest integer value that is less than or equal to the algebraic quotient. It firstly divides the dividend and divisor and then returns an integer that is less or equal to the quotient.
Syntax:
- public static int floorDiv(int x, int y)
- public static long floorDiv(long x, int y)
- public static long floorDiv(long x, long y)
Parameters:
The parameter ‘x’ and ‘y’ represents the dividend and the divisor.
Return Value:
The floorDiv () method returns the largest integer value that is less than or equal to the algebraic quotient.
- It returns the result same as the first argument, if the first argument passed is equal to an integer or long value and second argument is -1.
- It throws an Arithmetic Exception, if the divisor is zero.
Example 1:
public class JavaMathFloorDivExample1 { public static void main(String[] args) { int x = 2125; int y= 10; // divides x and y(x/y) and returns the integer less than or equal to quotient. System.out.println(Math.floorDiv(x, y)); } }
Output:
212
Example 2:
public class JavaMathFloorDivExample2 { public static void main(String[] args) { int x = 2125; long y= 100; // divides x and y(x/y) and returns the integer less than or equal to quotient. System.out.println(Math.floorDiv(x, y)); } }
Output:
21
Example 3:
public class JavaMathFloorDivExample3 { public static void main(String[] args) { long x = -2125; long y= 1; //it returns an Airthmetic Exception, if the Second argument is zero System.out.println(Math.floorDiv(x, y)); } }
Output:
-2125
Example 4:
public class JavaMathFloorDivExample4 { public static void main(String[] args) { long x = -2125; long y= -1; //it y is -1 then it will alter the sign of x System.out.println(Math.floorDiv(x, y)); } }
Output:
2125
Example 5:
public class JavaMathFloorDivExample5 { public static void main(String[] args) { int x = Integer.MIN_VALUE; int y= -1; /*if the dividend is the Integer.MIN_VALUE and the divisor is -1, then integer overflow occurs and the result is equal to Integer. MIN_VALUE */ System.out.println(Math.floorDiv(x, y)); } }
Output:
-2147483648