Java tutorialLesson 5 of 18 · tap to see all lessons
Java tutorial · Beginner
Java Operators and Expressions: Arithmetic, Logical, Increment, and Precedence
Use Java's arithmetic, assignment, relational, logical, and unary operators, understand ++ and --, and predict results using operator precedence.
- Qyubie Learn Editorial Team
- Published
- 8 min read
Includes 3 quick checks you can do right here.
Operators are the symbols that do the work in an expression: add, compare, combine conditions, and count up. Most look like the math you know, but a few behave in ways that surprise beginners, especially integer division, ++, and the order of operations. Those three show up on almost every exam.
By the end of this lesson you will be able to:
- use arithmetic and assignment operators, including
%, - compare values with relational operators,
- combine conditions with
&&,||, and!, - explain the difference between
x++and++x, and - predict results using operator precedence.
Arithmetic operators
| Operator | Meaning | 17 ? 5 |
|---|---|---|
+ | addition | 22 |
- | subtraction | 12 |
* | multiplication | 85 |
/ | division | 3 (both are int) |
% | remainder (modulo) | 2 |
When both sides of / are integers, Java does integer division: it drops the remainder. If either side is a double, you get decimals.
System.out.println(17 / 5);
System.out.println(17 % 5);
System.out.println(17 / 5.0);
System.out.println(-17 / 5);Output:
3
2
3.4
-3Integer division always cuts toward zero, which is why -17 / 5 is -3, not -4.
% is handy for checking divisibility: n % 2 == 0 means n is even, and n % 10 gives the last digit.
Quick check
Assignment operators
= stores a value. The compound forms update a variable using its current value:
| 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 |
int score = 10;
score += 5;
score *= 2;
score -= 4;
score /= 3;
System.out.println(score);Output:
8Step by step: 10, then 15, 30, 26, and finally 26 / 3 = 8 (integer division).
Relational operators
These compare two values and produce a boolean, either true or false.
| Operator | Meaning |
|---|---|
== | equal to |
!= | not equal to |
> < | greater than, less than |
>= <= | at least, at most |
int grade = 85;
System.out.println(grade >= 75);
System.out.println(grade == 100);
System.out.println(grade != 0);Output:
true
false
true= assigns and == compares. Writing if (grade = 75) is a compile error in Java, which at least catches the mistake for you.
For Strings, use .equals(), not ==. We'll see why in the next lesson.
Logical operators
Logical operators combine boolean values:
| Operator | Name | True when |
|---|---|---|
&& | AND | both sides are true |
|| | OR | at least one side is true |
! | NOT | flips true and false |
int age = 19;
boolean enrolled = true;
System.out.println(age >= 18 && enrolled);
System.out.println(age < 18 || !enrolled);
System.out.println(!(age >= 18));Output:
true
false
falseShort-circuit evaluation. && stops as soon as the left side is false, because the result can't be true anymore. || stops as soon as the left side is true. This lets you guard risky code:
int count = 0;
int total = 50;
if (count != 0 && total / count > 10) {
System.out.println("High average");
} else {
System.out.println("No data yet");
}Output:
No data yetBecause count != 0 is false, Java never evaluates total / count, so there's no division-by-zero crash.
Unary operators, ++ and --
A unary operator works on one value: -x (negate), !flag (not), and the increment and decrement operators:
x++and++xboth add 1 tox.x--and--xboth subtract 1.
The difference only matters when the expression's value is used:
- Postfix
x++: use the old value, then add 1. - Prefix
++x: add 1 first, then use the new value.
int a = 5;
int b = a++; // b gets 5, then a becomes 6
int c = 5;
int d = ++c; // c becomes 6, then d gets 6
System.out.println(a + " " + b);
System.out.println(c + " " + d);Output:
6 5
6 6On a line by itself (count++;), both forms do the same thing. Exams like to mix them into bigger expressions, so read them carefully.
Quick check
Operator precedence
When an expression has several operators, Java follows a fixed order, highest first:
| Order | Operators |
|---|---|
| 1 | () parentheses |
| 2 | unary: ++ -- ! - (negative) and casts like (int) |
| 3 | * / % |
| 4 | + - |
| 5 | < <= > >= |
| 6 | == != |
| 7 | && |
| 8 | || |
| 9 | = += -= and the other assignments |
Operators on the same level go left to right, except assignment, which goes right to left.
System.out.println(2 + 3 * 4);
System.out.println((2 + 3) * 4);
System.out.println(10 - 4 - 3);
System.out.println(20 / 2 * 5);
System.out.println(1 + 2 + "3" + 4 + 5);Output:
14
20
3
50
3345The last line mixes numbers and Strings. Left to right: 1 + 2 is 3, then 3 + "3" becomes the String "33", and from then on every + joins text: "334", then "3345".
Quick check
When in doubt, add parentheses. They make your intent clear to both Java and the next person reading your code.
Common mistakes
- Expecting decimals from
int / int.7 / 2is3. Write7 / 2.0or cast one side. - Using
=instead of==in a condition. - Using
==to compare Strings. Use.equals(). - Forgetting precedence:
(a + b) / 2anda + b / 2are different. - Writing
&or|instead of&&or||. The single forms don't short-circuit.
Summary
/on twoints drops the remainder, and%gives the remainder.- Compound assignment (
+=,-=, …) updates a variable in place. - Relational operators produce
booleans;&&,||, and!combine them, with short-circuiting. x++uses the old value, then adds 1;++xadds 1, then uses the new value.- Precedence: parentheses, unary,
* / %,+ -, comparisons,&&,||, then assignment.
Next lesson: selection structures: making decisions with if, else if, and switch.