Operators · Lesson 8 of 42
Arithmetic, Assignment and Increment Operators
Operators are the verbs of a program: they add, compare, combine and assign. This lesson covers the arithmetic family – including the famous i++ vs ++i question that appears in almost every Java written test.
In this lesson
+ - * / %and integer vs decimal division- Increment and decrement: prefix vs postfix
- Compound assignment operators
- Useful
Mathmethods and operator precedence
The arithmetic operators
public class Main {
public static void main(String[] args) {
int a = 17, b = 5;
System.out.println(a + b); // 22
System.out.println(a - b); // 12
System.out.println(a * b); // 85
System.out.println(a / b); // 3 integer division
System.out.println(a % b); // 2 remainder
System.out.println(a / 5.0); // 3.4 one double → double result
System.out.println(-17 % 5); // sign follows the left operand
}
}
22 12 85 3 2 3.4 -2
- Dividing two integers gives an integer – the fraction is dropped (not rounded).
%gives the remainder. In Java the result has the sign of the left operand, so-17 % 5is-2(Python would give 3).- Integer division by zero throws
ArithmeticException; floating-point division by zero givesInfinityorNaN.
public class Main {
public static void main(String[] args) {
System.out.println(10.0 / 0); // Infinity
System.out.println(0.0 / 0); // NaN
System.out.println(10 / 0); // throws
}
}
Infinity NaN Exception in thread "main" java.lang.ArithmeticException: / by zero
Modulo in practice
public class Main {
public static void main(String[] args) {
int n = 4729;
System.out.println(n % 2 == 0 ? "even" : "odd");
System.out.println("last digit: " + n % 10);
System.out.println("without last digit: " + n / 10);
int seconds = 3725;
System.out.printf("%d h %d min %d s%n", seconds / 3600, seconds % 3600 / 60, seconds % 60);
}
}
odd last digit: 9 without last digit: 472 1 h 2 min 5 s
Increment and decrement
++ adds 1 and -- subtracts 1. Both come in two forms, and the difference matters only when the result is used in the same expression:
Prefix vs postfix
x = 5
- y = ++x (prefix)increment first, then use → x = 6, y = 6
- y = x++ (postfix)use the old value, then increment → x = 6, y = 5
public class Main {
public static void main(String[] args) {
int x = 5;
int y = ++x;
System.out.println("x=" + x + " y=" + y);
x = 5;
y = x++;
System.out.println("x=" + x + " y=" + y);
int i = 10;
int r = i++ + ++i; // 10 + 12
System.out.println("r=" + r + " i=" + i);
}
}
x=6 y=6 x=6 y=5 r=22 i=12
Common mistake: x = x++
int x = 5; x = x++; leaves x at 5. The old value (5) is saved, x becomes 6, then the assignment writes the saved 5 back. Tricky output questions love this – in real code, never mix ++ with other operations on the same variable.
Compound assignment
public class Main {
public static void main(String[] args) {
int balance = 1000;
balance += 500; // balance = balance + 500
balance -= 200;
balance *= 2;
balance /= 3;
balance %= 400;
System.out.println(balance);
String msg = "Hi";
msg += " there"; // works for strings too
System.out.println(msg);
}
}
66 Hi there
(1000 + 500 − 200 = 1300; × 2 = 2600; ÷ 3 = 866; 866 % 400 = 66.)
The Math class
public class Main {
public static void main(String[] args) {
System.out.println(Math.max(12, 30) + " " + Math.min(12, 30));
System.out.println(Math.abs(-8) + " " + Math.pow(2, 10) + " " + Math.sqrt(144));
System.out.println(Math.round(4.5) + " " + Math.ceil(4.1) + " " + Math.floor(4.9));
System.out.println(Math.floorMod(-17, 5)); // always non-negative for positive divisor
int dice = (int) (Math.random() * 6) + 1; // 1..6
System.out.println(dice >= 1 && dice <= 6);
}
}
30 12 8 1024.0 12.0 5 5.0 4.0 3 true
Operator precedence
Higher rows are evaluated first. Operators on the same row run left to right (except assignment, which runs right to left).
| Priority | Operators |
|---|---|
| 1 (highest) | x++ x-- |
| 2 | ++x --x + - ! ~ (cast) (unary) |
| 3 | * / % |
| 4 | + - |
| 5 | << >> >>> |
| 6 | < <= > >= instanceof |
| 7 | == != |
| 8–10 | &, then ^, then | |
| 11–12 | &&, then || |
| 13 | ? : |
| 14 (lowest) | = += -= *= /= … |
public class Main {
public static void main(String[] args) {
System.out.println(2 + 3 * 4);
System.out.println((2 + 3) * 4);
System.out.println(10 - 4 - 3); // left to right
System.out.println(1 + 2 + "3" + 4 + 5);
}
}
14 20 3 3345
The last line: 1 + 2 is 3, then 3 + "3" becomes the string "33", and everything after is joined as text.
Practice
Try it yourself
Reverse the number 1234 using
%and/in a loop.Show solution
public class Main { public static void main(String[] args) { int n = 1234, rev = 0; while (n > 0) { rev = rev * 10 + n % 10; n /= 10; } System.out.println(rev); } }4321
Predict, then run:
int a = 3; int b = a++ * 2 + --a;– what are a and b?Show solution
public class Main { public static void main(String[] args) { int a = 3; int b = a++ * 2 + --a; // 3 * 2 + 3 System.out.println(a + " " + b); } }3 9
Quick check
What is 7 / 2 in Java? And 7 / 2.0?
3 and 3.5.
What is -7 % 3?
-1 – the result takes the sign of the left operand.
After int x = 5; int y = x++; what are x and y?
x = 6, y = 5.
Summary
- Integer division drops the fraction;
%takes the sign of the left operand. ++xincrements then uses;x++uses then increments.- Compound operators (
+=…) shorten updates; brackets beat memorising precedence.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.