Java Operators and Expressions
In this lesson
Operators are the symbols that do work on values: add, compare, combine conditions. You’ll use them in every line of logic you write.
Arithmetic operators#
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | add | 7 + 3 | 10 |
- | subtract | 7 - 3 | 4 |
* | multiply | 7 * 3 | 21 |
/ | divide | 7 / 3 | 2 (int division) |
% | remainder (modulus) | 7 % 3 | 1 |
% is incredibly useful: n % 2 == 0 checks for even numbers, n % 10 gives the last digit.
public class Digits {
public static void main(String[] args) {
int n = 4729;
System.out.println("Last digit: " + n % 10); // 9
System.out.println("Without last: " + n / 10); // 472
System.out.println("Is even? " + (n % 2 == 0)); // false
}
}
Assignment and compound assignment#
int score = 10;
score += 5; // score = score + 5 → 15
score -= 3; // 12
score *= 2; // 24
score /= 5; // 4
score %= 3; // 1
Increment and decrement#
int a = 5;
int b = a++; // b = 5, then a becomes 6 (post-increment)
int c = ++a; // a becomes 7, then c = 7 (pre-increment)
System.out.println(a + " " + b + " " + c); // 7 5 7
Post (a++) uses the old value then increments; pre (++a) increments first. Interviewers love asking this. In real code, use a++ on its own line to keep things clear.
Comparison operators#
All return a boolean:
| Operator | Meaning |
|---|---|
== | equal to |
!= | not equal |
> < | greater / less than |
>= <= | greater or equal / less or equal |
Use == for primitives only. To compare Strings or other objects, use .equals(). You’ll see exactly why in the Strings lesson.
Logical operators#
| Operator | Meaning | True when |
|---|---|---|
&& | AND | both sides are true |
|| | OR | at least one side is true |
! | NOT | flips true/false |
int age = 20;
boolean hasId = true;
boolean canEnter = age >= 18 && hasId; // true
boolean discount = age < 12 || age > 60; // false
System.out.println(canEnter + " " + discount + " " + !canEnter);
Short-circuit evaluation#
&& stops as soon as the left side is false; || stops as soon as the left side is true. This protects you from errors:
String name = null;
if (name != null && name.length() > 3) { // safe: length() never runs when name is null
System.out.println("Long name");
}
The ternary operator#
A one-line if/else that returns a value:
int marks = 72;
String result = marks >= 40 ? "Pass" : "Fail";
System.out.println(result); // Pass
Format: condition ? valueIfTrue : valueIfFalse. Great for simple choices; use a normal if for anything complex.
Operator precedence#
Like maths, some operators go first:
()brackets++--!(unary)*/%+-<><=>===!=&&||?:=+=…
System.out.println(2 + 3 * 4); // 14, not 20
System.out.println((2 + 3) * 4); // 20
System.out.println("Sum: " + 2 + 3); // Sum: 23 (text joining goes left to right!)
System.out.println("Sum: " + (2 + 3)); // Sum: 5
When in doubt, add brackets. They make code clearer for humans too.
The Math class#
public class MathDemo {
public static void main(String[] args) {
System.out.println(Math.max(12, 30)); // 30
System.out.println(Math.min(12, 30)); // 12
System.out.println(Math.abs(-7)); // 7
System.out.println(Math.pow(2, 10)); // 1024.0
System.out.println(Math.sqrt(144)); // 12.0
System.out.println(Math.round(4.5)); // 5
System.out.println(Math.ceil(4.1)); // 5.0
System.out.println(Math.floor(4.9)); // 4.0
int dice = (int) (Math.random() * 6) + 1; // 1 to 6
System.out.println(dice >= 1 && dice <= 6); // true
}
}
Common mistakes#
if (x = 5)→ assignment, not comparison. Java gives a compile error for ints (good!). Use==.- Expecting
5 / 2to be2.5. Use5 / 2.0or cast one side todouble. - Mixing
&with&&: single&doesn’t short-circuit. "Total: " + a + bjoins text instead of adding. Wrap the sum in brackets.
Exercise#
- Given seconds = 3725, print it as
1 h 2 min 5 susing/and%. - Write an expression that is true when a year is a leap year: divisible by 4 and not by 100, unless divisible by 400.
- Use the ternary operator to print “Adult” or “Minor”.
- Predict the output:
int x = 3; int y = x++ + ++x;What are x and y?
Hint for 2
(year % 4 == 0 && year % 100 != 0) || year % 400 == 0
Hint for 4
x++ gives 3 (x becomes 4), ++x makes x 5 and gives 5. So y = 8, x = 5.
Interview corner#
- **
i++vs++i?** Post-increment returns the old value; pre-increment returns the new one. - **
&&vs&?**&&short-circuits;&always evaluates both sides (and also works as bitwise AND on ints). - **What is
"5" + 3 + 2?**"532". And3 + 2 + "5"?"55". Evaluation is left to right.
Summary#
- Arithmetic:
+ - * / %; watch for integer division. - Comparisons return booleans; logical
&&,||,!combine them with short-circuiting. - Ternary
?:picks between two values; brackets beat memorising precedence. Mathhas max, min, abs, pow, sqrt, round, random and more.
Next: Strings, the most used class in Java.
Quick quiz
3 questions. Check what stuck and earn 2 XP for each right answer.
Progress is saved in this browser. Create a free account to keep it on every device.
Get new lessons in your inbox
One email when we publish a new tutorial or guide. No spam, unsubscribe in one click.