Operators
Operators perform calculations and comparisons: adding numbers, checking whether one value is greater than another, combining conditions. This lesson covers arithmetic, assignment, increment, comparison, logical and ternary operators, and the precedence rules that decide which runs first.
Arithmetic
void main() { int a = 17, b = 5; System.out.println(a + b); System.out.println(a - b); System.out.println(a * b); System.out.println(a / b); System.out.println(a % b); }
22 12 85 3 2
% (modulus) gives the remainder after division — useful for checking even numbers (n % 2 == 0) or wrapping around. Integer division by zero throws an ArithmeticException; with doubles it gives Infinity or NaN instead.
Compound assignment
void main() { int total = 10; total += 5; total -= 3; total *= 2; total /= 4; total %= 4; System.out.println(total); }
2
x += 5 is short for x = x + 5. (It also casts back to the variable's type automatically, so byteVar += 1 compiles where byteVar = byteVar + 1 doesn't.)
Increment and decrement
void main() { int count = 5; count++; System.out.println(count); int a = 5; int b = a++; System.out.println(a + " " + b); int c = 5; int d = ++c; System.out.println(c + " " + d); }
6 6 5 6 6
Both a++ and ++a add one. The difference is the value of the expression: a++ evaluates to the old value, ++a to the new one. On a line by itself, there's no difference; inside larger expressions it's easy to misread, so avoid mixing them in.
Comparison
void main() { int x = 7; System.out.println(x == 7); System.out.println(x != 7); System.out.println(x > 3); System.out.println(x <= 6); }
true false true false
Comparisons produce a boolean. Note == (compare) vs = (assign). For objects such as strings, == checks whether two references point to the same object — use .equals() to compare contents (lesson 9).
Logical operators
void main() { int age = 25; boolean hasId = true; System.out.println(age >= 18 && hasId); System.out.println(age < 12 || age > 65); System.out.println(!hasId); }
true false false
&& (and), || (or) and ! (not). && and || short-circuit: if the left side already decides the result, the right side isn't evaluated. That makes checks like name != null && name.isEmpty() safe.
The ternary operator
void main() { int stock = 0; String label = stock > 0 ? "In stock" : "Sold out"; System.out.println(label); }
Sold out
condition ? valueIfTrue : valueIfFalse picks one of two values. It's great for simple choices; use if for anything longer.
Precedence
void main() { System.out.println(2 + 3 * 4); System.out.println((2 + 3) * 4); System.out.println(10 - 4 - 3); System.out.println(true || false && false); }
14 20 3 true
| Priority | Operators |
|---|---|
| Highest | x++ x-- |
++x --x ! unary -, casts | |
* / % | |
+ - | |
< > <= >= instanceof | |
== != | |
&& | |
|| | |
?: | |
| Lowest | = += -= … |
Operators with equal priority mostly run left to right. When in doubt, add parentheses — they cost nothing and help readers.
Bitwise operators
void main() { int flags = 0b0101; System.out.println(flags & 0b0100); System.out.println(flags | 0b0010); System.out.println(flags ^ 0b0001); System.out.println(1 << 4); }
4 7 4 16
&, |, ^ (xor), ~, and the shifts <<, >>, >>> work on individual bits. You'll mostly meet them in low-level code, flags and hashing.
Try it yourself
Convert a number of seconds into hours, minutes and seconds.
2h 3m 5s