Open In App

Java Math floorDiv() Method

Last Updated : 13 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Syntax of floorDiv() Method

public static int floorDiv(int x, int y)

public static long floorDiv(long x, long y)

Parameters:

  • x: This is the number to be divided i.e., dividend.
  • y: This is the number by which the dividend is divided i.e., divisor.

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.

Examples of Java Math floorDiv() Method

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.

Java
// Java program to demonstrate the use of Math.floorDiv()
import java.lang.Math;

public class Geeks {
    
    public static void main(String[] args) {
        
        int a = 25, b = 5;
        System.out.println("The result is: " + Math.floorDiv(a, b));

        // Example with non-divisible result
        int c = 125, d = 50;
        System.out.println("The result is: " + Math.floorDiv(c, d)); 
    }
}

Output
The result is: 5
The result is: 2

Explanation:

  • In the first case, 25 / 5 results in 5. The quotient is already an integer, so, the result of Math.floorDiv(25, 5) is also 5.
  • In the second case, 125 / 50 results in 2.5. So, the method rounds the result down to the nearest integer less than or equal to the quotient. So, it gives 2 i.e., the result that is less than or equal to the quotient.

Example 2: In this example, we will see what happens when we attempt to divide by zero using Math.floorDiv().

Java
// Java program to demonstrate ArithmeticException
import java.lang.Math;

public class Geeks {
    
    public static void main(String[] args) {
        int x = 20, y = 0;

        System.out.println("The result is: " + Math.floorDiv(x, y));
    }
}

Output:

Exception in thread "main" java.lang.ArithmeticException: / by zero


When to Use Math.floorDiv() Instead of the Standard Division (/)

We should use Math.floorDiv() when:

  • We need consistent rounding down behavior, when working with negative numbers.
  • We want the result to follow the "floor" approach, rounding towards negative infinity, rather than truncating the decimal part which is what the standard division operator / does.
  • We are working with modular arithmetic, where we need to ensure results conform to specific constraints such as rounding downward.

Practice Tags :

Similar Reads