Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The * operator in Java is a binary arithmetic operator that performs multiplication on two numeric operands. It calculates the product of its left and right operands, strictly adhering to Java’s rules for binary numeric promotion and IEEE 754 arithmetic standards.
Type result = operand1 * operand2;

Operand Types

The * operator accepts operands of any primitive numeric type (byte, short, char, int, long, float, double). It also accepts their corresponding object wrapper classes (e.g., Integer, Double), which the Java compiler automatically unboxes into their primitive equivalents before evaluation. It cannot be applied to boolean or non-wrapper reference types.

Binary Numeric Promotion

Before the multiplication occurs, Java applies binary numeric promotion to equalize the operand types. The promotion follows a strict hierarchy:
  1. If either operand is of type double, the other is promoted to double. The result is a double.
  2. Otherwise, if either operand is float, the other is promoted to float. The result is a float.
  3. Otherwise, if either operand is long, the other is promoted to long. The result is a long.
  4. Otherwise, both operands are promoted to int (this includes byte, short, and char). The result is an int.
byte a = 10;
byte b = 5;
// result is of type int, not byte
int result = a * b; 

Precedence and Associativity

The * operator has multiplicative precedence. It is evaluated after unary operators (like ++ or !) but before additive operators (+ and -). It features left-to-right associativity, meaning an expression with multiple multiplicative operators (*, /, %) is evaluated from left to right.
// Evaluated as (a * b) * c
int result = a * b * c; 

Overflow Behavior

Java handles multiplication overflow differently depending on whether the operands are integers or floating-point numbers:
  • Integer Arithmetic (int, long): If the mathematical product exceeds the maximum or minimum bounds of the resulting type, the operation silently wraps around using two’s complement arithmetic. It does not throw an ArithmeticException.
  • Floating-Point Arithmetic (float, double): If the product exceeds the maximum representable finite value, the result overflows to positive or negative Infinity, depending on the signs of the operands.

IEEE 754 Special Cases

When multiplying floating-point numbers, the * operator follows IEEE 754 specifications for special values:
  • NaN (Not a Number): If either operand is NaN, the result is NaN.
  • Infinity by Zero: Multiplying positive or negative Infinity by 0 or 0.0 results in NaN.
  • Infinity by Finite: Multiplying Infinity by any non-zero finite value results in Infinity. The sign of the resulting Infinity is positive if both operands have the same sign, and negative if they have different signs.
  • Signed Zeros: Java supports -0.0 and +0.0. Multiplying two zeros yields a zero, with the sign determined by the standard XOR sign rules (e.g., -0.0 * +0.0 yields -0.0).
Master Java with Deep Grasping Methodology!Learn More