Syntax
+ - * / % += -= ++ --Examples
Basic Arithmetic
The core arithmetic operators, including integer division behavior.
int a = 10, b = 3;
System.out.println(a + b); // 13
System.out.println(a - b); // 7
System.out.println(a * b); // 30
System.out.println(a / b); // 3 (integer division truncates)
System.out.println(a % b); // 1 (remainder)
double result = 10.0 / 3; // true division with a double
System.out.println(result); // 3.3333333333333335Compound Assignment Operators
Shorthand operators that update a variable based on its current value.
int score = 10;
score += 5; // score = score + 5 -> 15
score -= 3; // 12
score *= 2; // 24
score /= 4; // 6
System.out.println(score);Increment and Decrement
The difference between prefix (++x) and postfix (x++) forms.
int x = 5;
System.out.println(x++); // prints 5, THEN increments (x is now 6)
System.out.println(x); // 6
int y = 5;
System.out.println(++y); // increments FIRST, then prints (y is now 6)
System.out.println(y); // 6Best practices
- Watch out for integer division truncating results - divide by a double literal (10.0 / 3) when you need a precise decimal result
- Be careful mixing prefix and postfix increment operators within the same complex expression - it can make code hard to reason about
- Use compound assignment operators (+=, -=, etc.) for conciseness when updating a variable based on itself
- Remember % gives the remainder, not a true modulus - it can return negative results when the left-hand operand is negative
Runtime & versions
Java Virtual Machine. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Java Language Specification