Java Operators
Operators are symbols that perform operations on values (called operands). Java groups them into arithmetic, assignment, comparison, logical and a few special operators.
Arithmetic operators
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
addition | 7 + 2 |
9 |
- |
subtraction | 7 - 2 |
5 |
* |
multiplication | 7 * 2 |
14 |
/ |
division | 7 / 2 |
3 (integers!) |
% |
remainder (modulo) | 7 % 2 |
1 |
++ |
increment | x++ |
adds 1 |
-- |
decrement | x-- |
subtracts 1 |
class Main {
public static void main(String[] args) {
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); // whole-number division
System.out.println(a % b); // what is left over
System.out.println(17.0 / 5); // a double operand -> decimal result
System.out.println(-17 % 5); // sign follows the left operand
}
}22
12
85
3
2
3.4
-2
Tip:
n % 2 == 0is the classic test for "isneven?".
Increment and decrement
x++ and ++x both add one to x. The difference only matters when the expression's value is used: postfix (x++) gives the old value, prefix (++x) gives the new value.
class Main {
public static void main(String[] args) {
int x = 5;
int a = x++; // a gets 5, then x becomes 6
int b = ++x; // x becomes 7, then b gets 7
System.out.println("a=" + a + " b=" + b + " x=" + x);
}
}a=5 b=7 x=7
Assignment operators
Compound assignment combines an operation with =:
| Operator | Same as |
|---|---|
x += 3 |
x = x + 3 |
x -= 3 |
x = x - 3 |
x *= 3 |
x = x * 3 |
x /= 3 |
x = x / 3 |
x %= 3 |
x = x % 3 |
class Main {
public static void main(String[] args) {
int balance = 100;
balance += 50;
balance -= 30;
balance *= 2;
balance /= 3;
System.out.println(balance);
String log = "start";
log += " -> saved";
System.out.println(log);
}
}80
start -> saved
Comparison and logical operators
Comparisons produce a boolean. Logical operators combine booleans:
| Operator | Meaning | Operator | Meaning |
|---|---|---|---|
== |
equal to | && |
AND — both true |
!= |
not equal | || |
OR — at least one true |
> < |
greater / less than | ! |
NOT — inverts |
>= <= |
greater/less or equal |
class Main {
public static void main(String[] args) {
int age = 20;
boolean hasTicket = true;
System.out.println(age >= 18);
System.out.println(age == 21);
System.out.println(age != 21);
System.out.println(age >= 18 && hasTicket);
System.out.println(age < 12 || age > 65);
System.out.println(!hasTicket);
}
}true
false
true
true
false
false
&& and || are short-circuit operators: if the left side already decides the result, the right side is not evaluated at all. This lets you write safe checks like name != null && name.length() > 3.
Note: Use
==for primitives. To compare the contents of two strings usea.equals(b)— see the Strings chapter.
The ternary operator
condition ? valueIfTrue : valueIfFalse is a compact if/else that produces a value:
class Main {
public static void main(String[] args) {
int stock = 0;
String label = stock > 0 ? "In stock" : "Sold out";
System.out.println(label);
int a = 8, b = 13;
System.out.println("max = " + (a > b ? a : b));
}
}Sold out
max = 13
Precedence
When an expression has several operators, Java follows precedence rules — roughly: ++ -- ! first, then * / %, then + -, then comparisons, then &&, then ||, then ?:, and finally assignment. 2 + 3 * 4 is 14, not 20. When in doubt, add parentheses; they make your intent obvious.
Test yourself
What is the result of 14 % 4?
- 3
- 3.5
- 2
- 0
After int x = 3; int y = x++ * 2;, what are x and y?
- x=4, y=8
- x=4, y=6
- x=3, y=6
- x=3, y=8
Why is s != null && s.isEmpty() safe when s is null?
- isEmpty handles null
- && short-circuits, so isEmpty is never called
- Java converts null to an empty string
- It is not safe