🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The Math.floorMod() method in Java is used to return the floor modulus of the given arguments.
Table of Contents
- Introduction
floorMod()Method Syntax- Overloaded
floorMod()Methods - Examples
floorMod(int x, int y)floorMod(long x, int y)floorMod(long x, long y)
- Real-World Use Case
- Conclusion
Introduction
The Math.floorMod() method returns the floor modulus of the given arguments. The floor modulus operation is similar to the modulus operation but ensures that the result is always non-negative.
floorMod() Method Syntax
The syntax for the floorMod() method varies depending on the types of the arguments:
floorMod(int x, int y)
public static int floorMod(int x, int y)
floorMod(long x, int y)
public static int floorMod(long x, int y)
floorMod(long x, long y)
public static long floorMod(long x, long y)
Parameters:
x: The dividend.y: The divisor.
Returns:
- The floor modulus of the given arguments.
Throws:
ArithmeticExceptionif the divisoryis zero.
Overloaded floorMod() Methods
The Math.floorMod() method is overloaded to handle different combinations of primitive data types: int and long. Each version returns the floor modulus of the given arguments.
Examples
floorMod(int x, int y)
The floorMod(int x, int y) method returns the floor modulus of two int values.
Example
public class FloorModIntExample {
public static void main(String[] args) {
int x1 = 7, y1 = 3;
int x2 = -7, y2 = 3;
int result1 = Math.floorMod(x1, y1);
int result2 = Math.floorMod(x2, y2);
System.out.println("Floor modulus of " + x1 + " % " + y1 + " is " + result1);
System.out.println("Floor modulus of " + x2 + " % " + y2 + " is " + result2);
}
}
Output:
Floor modulus of 7 % 3 is 1
Floor modulus of -7 % 3 is 2
floorMod(long x, int y)
The floorMod(long x, int y) method returns the floor modulus of a long and an int value.
Example
public class FloorModLongIntExample {
public static void main(String[] args) {
long x1 = 10L, y1 = 3;
long x2 = -10L, y2 = 3;
int result1 = Math.floorMod(x1, y1);
int result2 = Math.floorMod(x2, y2);
System.out.println("Floor modulus of " + x1 + " % " + y1 + " is " + result1);
System.out.println("Floor modulus of " + x2 + " % " + y2 + " is " + result2);
}
}
Output:
Floor modulus of 10 % 3 is 1
Floor modulus of -10 % 3 is 2
floorMod(long x, long y)
The floorMod(long x, long y) method returns the floor modulus of two long values.
Example
public class FloorModLongLongExample {
public static void main(String[] args) {
long x1 = 20L, y1 = 4L;
long x2 = -20L, y2 = 4L;
long result1 = Math.floorMod(x1, y1);
long result2 = Math.floorMod(x2, y2);
System.out.println("Floor modulus of " + x1 + " % " + y1 + " is " + result1);
System.out.println("Floor modulus of " + x2 + " % " + y2 + " is " + result2);
}
}
Output:
Floor modulus of 20 % 4 is 0
Floor modulus of -20 % 4 is 0
Real-World Use Case
Handling Negative Values
In real-world scenarios, the Math.floorMod() method can be used to ensure that the result of a modulus operation is always non-negative, which is useful in applications like array indexing or circular buffers where negative indices are not valid.
Example
public class CircularBufferExample {
public static void main(String[] args) {
int bufferSize = 5;
int[] buffer = new int[bufferSize];
int index = -3;
int validIndex = Math.floorMod(index, bufferSize);
System.out.println("Valid index for buffer is " + validIndex);
}
}
Output:
Valid index for buffer is 2
Conclusion
The Math.floorMod() method in Java provides a way to perform modulus operations and ensure that the result is always non-negative.
By understanding how to use this method and its overloaded versions, you can handle various modulus operations and solve problems that require a non-negative result.
Whether you are working with integers or long integers, the floorMod() method offers a reliable tool for ensuring correct and non-negative modulus results.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment