×

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:

  1. public static int floorDiv(int x, int y)
  2. public static long floorDiv(long x, int y)
  3. 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

Related Topics

Java Arrays

An array is an object that stores a collection of values. An array can store two types of collection data: objects and primitives. An array is a collection of values which...

2 minutes read.

Java Thread Dump Analyzer

Thread: A thread is a PC program that is stacked into the PC's memory and is under execution. It tends to be executed by a processor or a bunch of processors....

15 minutes read.

Coin change problem in dynamic programming

In this tutorial, we will understand a popular problem called the coin changeproblem through dynamic programming. This problem checks the logical and critical thinking ability of the person. Dynamic Programming...

5 minutes read.

How to Convert String to double in Java

How to Convert String to double in java It is used if we have to perform mathematical operations on the string that contains a double number. When we get data from...

3 minutes read.

Heap Implementation in Java

The root node or parent node of a Java heap is compared to its left and right offspring, and the children are then ordered in line with the comparison.Assuming that...

9 minutes read.

Magic Square in Java

A square with numbers is referred to as a magic square. The numbers in a magic square of order n are arranged to ensure that the sum of all of...

4 minutes read.

Java vs JavaScript

Java Vs JavaScript Java and JavaScript, both play an important role in the field of Computer Technology. Most people think JavaScript is a part of Java. But it is not entirely...

4 minutes read.

Java 9 Interface Private Method

Java 9 provides us the facility to include private methods inside the java interface. In java 8 and earlier versions, we are supposed to use only constant variables and abstract...

3 minutes read.

Various Operation on Queue using Linked List in Java

We keep track of front and rear pointers in a queue data structure. The head of the line points to the first item, and the back to the last. Rear is...

3 minutes read.

How to Iterate List in Java?

List is a Collection foundation interface in Java. It enables us to keep the collection of objects in order. The four classes that make up the List interface implementation are...

3 minutes read.

List of Constants in Java

In this tutorial, we are going to deal with constants available in Java.Every programming language has its constants. Similarly, Javaalso has got constants of its own. In this tutorial, we will...

6 minutes read.

How to Convert Timestamp to Date in Java

How to Convert Timestamp to Date in Java You can convert Timestamp to Date by using the constructor of Date class. It returns the long millisecond from Epoch (1st January 1970)...

2 minutes read.

Session Tracking in Java

When a series of requests from the same User (i.e., requests coming from the same browser) occurs over an extended period of time, servlets employ a mechanism known as session...

3 minutes read.

Java finalize()

Java finalize() In Java, the Object class is the root class that is inherited by all the Java classes. The class provides the finalize() method that is called just before the...

4 minutes read.

Java Thread class

Thread class The thread represents a part of the process. Every process can have multiple associated threads in which every thread may execute the same or different job. By default, each thread assigns...

16 minutes read.

How to add double quotes in a string in Java

Strings are indicated using double-quotes. Double quotes are not printed; the values inside the double quotes are printed. There are different methods for adding double quotes to the string, such...

2 minutes read.

Grepcode Java util date

What is java.util.Date Class? The date and time in Java are provided through the java.util.Date class. If you imported java.util, it could be helpful. Use the Java.util.Date class to implement this class...

4 minutes read.

Shallow copy in Java

Java's most important task is making a copy or clone of an object. In this part, we'll talk about shallow copies in Java and how to make them of Java...

4 minutes read.

Java Append Data to File

When writing data to a file using the classes within the java.io package, its file will often be overwritten, meaning that any existing data will be removed and new data...

4 minutes read.

Java Garbage Collection Interview Questions

One of the key areas of Java is garbage collection. Garbage collection enables apps to manage memory automatically. Interviewers frequently ask inquiries about garbage collection. Q1: What is the purpose of...

6 minutes read.