Basics Lesson 6 of 21
Java Operators
Arithmetic, assignment, increment, comparison and logical operators in Java, with operator precedence and the difference between x++ and ++x.
Operators are the symbols that do work on values: adding prices, comparing grades, combining conditions. Most will look familiar from math class.
Arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Add | 10 + 3 | 13 |
- | Subtract | 10 - 3 | 7 |
* | Multiply | 10 * 3 | 30 |
/ | Divide | 10 / 3 | 3 (int division) |
% | Remainder | 10 % 3 | 1 |
Java has no power operator; use Math.pow(2, 10) instead (see the Math lesson).
public class Main {
public static void main(String[] args) {
int totalMinutes = 135;
int hours = totalMinutes / 60;
int minutes = totalMinutes % 60;
System.out.println(hours + " hr " + minutes + " min");
int students = 43;
System.out.println("Groups of 5: " + students / 5 + ", left over: " + students % 5);
}
}
Output
2 hr 15 min Groups of 5: 8, left over: 3
Assignment operators
| Operator | Same as |
|---|---|
x += 5 | x = x + 5 |
x -= 5 | x = x - 5 |
x *= 5 | x = x * 5 |
x /= 5 | x = x / 5 |
x %= 5 | x = x % 5 |
Increment and decrement
x++ adds 1 and x-- subtracts 1. There's a subtle difference between putting the operator after (postfix) or before (prefix):
public class Main {
public static void main(String[] args) {
int a = 5;
int b = a++; // b gets 5, then a becomes 6
System.out.println("a=" + a + ", b=" + b);
int c = 5;
int d = ++c; // c becomes 6 first, then d gets 6
System.out.println("c=" + c + ", d=" + d);
}
}
Output
a=6, b=5 c=6, d=6
Comparison operators
These compare two values and give a boolean:
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
> / < | Greater / less than |
>= / <= | Greater / less than or equal |
Logical operators
| Operator | Meaning | True when… |
|---|---|---|
&& | AND | both are true |
|| | OR | at least one is true |
! | NOT | the value is false |
public class Main {
public static void main(String[] args) {
double average = 92.5;
int lowest = 86;
int age = 17;
boolean registered = false;
boolean withHonors = average >= 90 && lowest >= 85;
boolean canVote = age >= 18 && registered;
boolean getsDiscount = age < 18 || age >= 60;
System.out.println("With honors: " + withHonors);
System.out.println("Can vote: " + canVote);
System.out.println("Discount: " + getsDiscount);
System.out.println("Not registered: " + !registered);
}
}
Output
With honors: true Can vote: false Discount: true Not registered: true
&& and || are short-circuit operators: if the left side already decides the answer, the right side isn't even checked. That makes count != 0 && total / count > 10 safe from division by zero.
Operator precedence
From highest to lowest priority:
- Parentheses
( ) ++,--,!*,/,%+,-<,>,<=,>===,!=&&||=,+=,-=…
When in doubt, add parentheses. (a + b) / 2 is always clearer than relying on rules.
The ternary operator
int grade = 79;
String remark = grade >= 75 ? "PASSED" : "FAILED";
condition ? valueIfTrue : valueIfFalse is a compact if-else that produces a value.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04