Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Remainder assignment operator %=

The %= operator calculates a remainder and stores it in the left operand, implicitly converting the result.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

variable %= expression;

variable %= expression stores the remainder in a writable variable, field, or array element. It supports numeric types, including floating-point types.

For left-operand type T, it corresponds to variable = (T) (variable % expression), but evaluates the left operand only once. The implicit conversion allows byte without an explicit cast.

A nonzero integer remainder has the dividend's sign, not necessarily positive. Integer % with a zero divisor throws ArithmeticException; floating-point % with zero produces NaN.

Examples

public class Main {
    public static void main(String[] args) {
        byte value = 17;
        value %= 5;
        int negative = -17;
        negative %= 5;
        double fraction = 7.5;
        fraction %= 2.0;
        System.out.println(value);
        System.out.println(negative);
        System.out.println(fraction);
    }
}

Result

2
-2
1.5

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources