Codectionary / Developer documentation / Java

Arithmetic & Assignment Operators

Java provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder (modulus). It also has compound assignment operators (+=, -=, etc.) that combine an operation with assignment, along with increment (++) and decrement (--) operators in both prefix and postfix forms, which subtly differ in when the value is updated relative to being used.

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.3333333333333335

Compound 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);    // 6

Best 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

At a glance

Purpose
General-purpose application development
File extension
.java
Runs in
Java Virtual Machine
Usually used with
JDK and Java libraries

Specifications & further reading

Related Java documentation

Variables & Primitive Data Types
Java is a statically-typed language, meaning every variable must be declared with an explicit type before use, and that type cannot change afterward. Java has eight primitive types - byte, short, int, long, float, double, char, and boolean - which store simple values directly rather than references, making them fast and memory-efficient. Anything beyond primitives (String, arrays, custom classes) is a reference type instead.
System.out.println / print / printf
System.out is Java's standard output stream, and it exposes three main methods for writing text to the console. println() prints its argument followed by a newline, print() prints without adding a newline, and printf() gives C-style formatted output using format specifiers like %d, %s, and %.2f for precise control over how values are displayed.
Comments & Javadoc
Java supports single-line comments with //, multi-line comments with /* */, and a special documentation format called Javadoc, written as /** */. Javadoc comments support tags like @param, @return, and @throws, and can be processed by the javadoc tool to automatically generate HTML API documentation - the same format used for Java's own official standard library docs.
Comparison & Logical Operators
Comparison operators (==, !=, <, >, <=, >=) compare two values and evaluate to a boolean. Logical operators (&&, ||, !) combine or invert boolean expressions, with && and || short-circuiting - meaning the second operand is skipped entirely if the first already determines the result. Importantly, == compares object references for non-primitive types like String, so .equals() should be used to compare their actual content.