introduction
In Java programming, when we mention "follow" and "model" operations, we usually think of%
This operator. However, although Java developers often mix the two, in fact, there are two different behaviors in the Java standard library that correspond to the traditional mathematical "model acquisition" and what we often call "overall acquisition".
1. Remainder Operation
In Java%
The operator performs the remaining operation. It calculates the remainder after dividing two integers. The specific rules are as follows:
// Example 1int a = 7; int b = 3; int remainder = a % b; // remainder = 1 // Example 2 (consider the negative number situation)int negativeA = -7; int sameB = 3; int negativeRemainder = negativeA % sameB; // negativeRemainder = -1
In the above example,7 % 3
The result is1
, this is because 7 is divided by 3 and 1 remains. And for-7 % 3
,turn out-1
, This is because the remainder of the negative number maintains the same sign as the dividend.
2. True Modulo Operation
In the field of mathematics, modulus operations ensure that the positive and negative nature of the result depends on the divisor rather than the divisor. The Java standard library provides a function()
To implement a true modulus operation, its results always have the same positive and negative nature as the divisor:
import ; // Example 3int modularResult = (7, 3); // modularResult = 1 (same as the remainder operation)// Example 4 (Compare the remainder operation)int modularNegativeResult = (-7, 3); // modularNegativeResult = 2
In this example,(-7, 3)
The result is2
, This is because when taking the modulus operation, Shang rounds in the negative infinite direction, that is,-7 / 3
The integer quotient is-2
, and then obtain the remainder based on the definition of modulus operation2
。
3. Comparison of differences
The main difference between the remainder operation and the modulus operation is the way of treating negative numbers:
-
Remaining operation (
%
): The symbol of the result depends on the divisor, and the absolute value of the result is ensured to be smaller than the absolute value of the divisor. -
Modal operation (
()
): The symbol of the result is the same as the divisor, and the size of the remainder value is also guaranteed to be smaller than the divisor. 4. Practical application
In actual programming, it is particularly important to understand these two concepts, especially in the fields of loops, array indexing, cryptography, etc. For example, when creating periodic animation effects, correctly using modulo fetching can avoid problems such as array out of bounds. At the same time, when dealing with loop boundaries within the negative range, modulo fetching operations can bring the expected loop effect.
To sum up, although in Java%
Operators usually meet most daily programming needs, but should be preferred if they need to meet the mathematical definition of modulo.()
method. I hope this article can help you better understand and apply the remaining and modulus operations in Java, and make correct decisions in actual projects.
This is the article about the concepts of dosage and modulus operations in Java and the difference in code practice. For more related contents of Java dosage and modulus operations, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!