Skip to main content
The /= (division assignment) operator is a compound assignment operator that divides the left-hand operand by the right-hand operand and assigns the resulting quotient back to the left-hand operand. The left-hand operand must be a variable (a LeftHandSide expression), not a literal or constant.

Syntax

Internal Mechanics and Equivalence

At compile time, the Java compiler translates the compound assignment E1 /= E2 into an explicit division and assignment, incorporating an implicit narrowing or widening primitive conversion. The operation is formally equivalent to:
Where T is the declared data type of E1. A critical distinction between E1 = E1 / E2 and E1 /= E2 is that in the compound assignment, the left-hand operand (E1) is evaluated exactly once. This is significant when the left operand contains side effects, such as method calls or post-increment array indices (e.g., array[i++] /= 2).

Type Casting Behavior

Because the operator automatically casts the result back to the type of the left operand, it suppresses compilation errors that would normally occur when assigning a wider type to a narrower type.

Division Semantics

The behavior of the /= operator depends entirely on the data types of the operands involved:
  • Integer Division (byte, short, char, int, long): If both operands resolve to integer types, the operation performs integer division, truncating any fractional part toward zero. If the right operand evaluates to 0, the JVM throws an ArithmeticException.
  • Floating-Point Division (float, double): If at least one operand is a floating-point type, the operation follows IEEE 754 arithmetic standards. Division by 0.0 does not throw an exception; instead, it results in Positive Infinity or Negative Infinity. Dividing a floating-point zero by zero results in NaN (Not a Number).
Tired of Poor Java Skills? Fix That With Deep Grasping!Learn More