Learn Java
import com.learnjava.operators.OpArithmetic;

Arithmetic Operators in Java

@Concept

Java's five arithmetic operators — +, -, *, /, and % — work on any numeric primitive, and + additionally works on String to mean concatenation. Division and modulo both behave differently depending on the operand types: integer division truncates toward zero, floating-point division produces a full decimal result, and modulo returns whatever is left over after division.

Beyond those five, ++ and -- are shorthand for adding or subtracting exactly one. Each comes in a prefix form (++x) and a postfix form (x++), and the difference is a real behavioral one, not just style: prefix updates the variable before the surrounding expression reads it, while postfix reads the old value first and updates afterward.

@HowItWorks
1

The five arithmetic operators

+, -, *, /, % cover addition, subtraction, multiplication, division, and remainder.

2

Plus does double duty

Between numbers it adds; between a String and anything else it concatenates, converting the other side to text automatically.

3

Integer vs floating-point division

Dividing two ints truncates toward zero; if either operand is a float or double, the division keeps its decimal part.

4

Modulo

% returns the remainder left over after division, useful well beyond just describing "what's left."

5

Increment and decrement

++ and -- add or subtract exactly one, and exist in both prefix (++x) and postfix (x++) forms.

6

Prefix versus postfix

Prefix updates the variable before the surrounding expression reads it; postfix reads the old value first and updates afterward.

@Example
public class ArithmeticDemo {
public static void main(String[] args) {
int x = 5;
System.out.println("Pre-increment: " + (++x));
int y = 5;
System.out.println("Post-increment: " + (y++));
System.out.println("y after post-increment: " + y);
}
}
Output
Pre-increment: 6
Post-increment: 5
y after post-increment: 6
1++x increments x to 6 first, then that 6 is what the println call actually receives.
2y++ uses the current value of y, which is 5, in the println call first, and only increments y to 6 afterward.
3By the time the third println runs, y has already become 6, confirming the increment did happen — just one statement later than with prefix.
@UseCases
Building a running total or counter inside a loop
Understanding why a[i++] and a[++i] can access different array elements
Debugging an off-by-one bug caused by mixing up prefix and postfix in the same expression
Concatenating numbers and text together into a single user-facing message

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.