Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Division assignment operator /=

The /= operator divides a variable’s saved value by the right operand and stores the result back, with an implicit conversion to the variable’s type.

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

Syntax

variable /= expression;

variable /= expression combines division with assignment. For left-hand type T, it corresponds to variable = (T) (variable / (expression)), except that the left-hand side is evaluated only once.

Integer division truncates toward zero. A floating-point operand causes floating-point division; subsequent conversion to the variable’s type can discard the fractional part. Thus, int narrowed = 9; narrowed /= 2.0; stores 4.

Integer division by zero throws ArithmeticException without storing a result. Floating-point division by zero can produce infinity or NaN instead of that exception.

Examples

public class Main {
    public static void main(String[] args) {
        int count = -9;
        count /= 2;
        double amount = 9.0;
        amount /= 2;
        int narrowed = 9;
        narrowed /= 2.0;
        System.out.println(count);
        System.out.println(amount);
        System.out.println(narrowed);
    }
}

Result

-4
4.5
4

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