BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Math.floorDiv() Method

By Chaitanya Singh | Filed Under: java

Java Math.floorDiv(int x, int y) method returns the integer quotient value after dividing argument x by argument y. This method divides the argument x by y and then applies the floor() method on the result to get the integer that is less than or equal to the original quotient value.

public class JavaExample
{
  public static void main(String[] args)
  {
    int x = 14;
    int y= 3;
    // Quotient of 14/3 is 4.67 so floor(4.67) = 4
    System.out.println(Math.floorDiv(x, y));
  }
}

Output:

4

Note: Do not confuse the floor() method with round() method. The round() returns the closest int value, however the floor always returns integer lower than the actual value.

Syntax of Math.floorDiv() method

Math.floorDiv(19, 4); //returns 4

floorDiv() Description

public static int floorDiv(int x, int y): Returns the floor of the quotient value, after dividing first argument x with the second argument y.

Note: The floor of a value is an integer number, which is less than or equal to the actual value.

All variations of this method:

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

floorDiv() Parameters

  • x: First argument, the dividend.
  • y: Second argument, the divisor.

floorDiv() Return Value

  • Returns the floor of the quotient.
  • If signs of both the arguments are same, then it returns the value which is equal to what we get using / operator.
  • If signs of arguments are different, then the quotient is negative. The floorDiv() returns the result less than or equal to the actual quotient value, whereas the / operator returns the result closer to zero. For example: floorDiv(-9 , 4) == -3, whereas (-9 / 4) == -2.
  • It throws ArithmeticException, if second argument is zero.

Example 1

public class JavaExample
{
  public static void main(String[] args)
  {
    int x = 19;
    int y= 4;
    int x2 = -19;
    int y2 = 4;
    System.out.println(Math.floorDiv(x, y));
    System.out.println(Math.floorDiv(x2, y2));
  }
}

Output:

Java Math.floorDiv() Example Output1

Example 2

public class JavaExample
{
  public static void main(String[] args)
  {
    int x = Integer.MIN_VALUE;
    int y= -1;
    System.out.println(Integer.MIN_VALUE);
    System.out.println(Math.floorDiv(x, y));
  }
}

Output:

Java Math.floorDiv() Example Output2

Example 3

public class JavaExample
{
  public static void main(String[] args)
  {
    int x = Integer.MAX_VALUE;
    int y = -1;
    System.out.println(Integer.MAX_VALUE);
    System.out.println(Math.floorDiv(x, y));
  }
}

Output:

Java Math.floorDiv() Example Output3

Example 4

public class JavaExample
{
  public static void main(String[] args)
  {
    int x = 20;
    int y = 0;
    System.out.println(Math.floorDiv(x, y));
  }
}

Output:

Java Math.floorDiv() Example Output4

Recommended Posts

  • Java Math.floor()
  • Java Math.expm1()
  • Java Math.toRadians()
  • Java Math.toDegrees()
❮ Java Math

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap