Home >Java >javaTutorial >The difference between / and % in java
/ is used for integer division operations and returns the quotient, while % is used for remainder operations and returns the remainder. For example: 10 / 3 = 3, 10 % 3 = 1, because the quotient of 10 divided by 3 is 3 and the remainder is 1.
The difference between / and % in Java
Short answer:
/
is used for integer division operations, and %
is used for remainder operations.
Detailed explanation:
Integer division operation (/)
/
Operator Performs an integer division operation in which the remainder of the dividend is discarded. Remainder operation (%)
%
operator performs the remainder operation, which returns the dividend divided by the divisor The remaining remainder. Example:
<code class="java">int dividend = 10; int divisor = 3; System.out.println("整除运算结果:" + (dividend / divisor)); // 输出 3 System.out.println("求余运算结果:" + (dividend % divisor)); // 输出 1</code>
Other notes:
ArithmeticException
exception will be thrown. By understanding the difference between the /
and %
operators, you can correctly perform division and remainder operations in your code.
The above is the detailed content of The difference between / and % in java. For more information, please follow other related articles on the PHP Chinese website!