Skip to content
Qyubie Learn
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

OperatorMeaning17 ? 5
+addition22
-subtraction12
*multiplication85
/division3 (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
-3

Integer 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

What does this print?
int minutes = 135;
System.out.println(minutes / 60 + "h " + minutes % 60 + "m");

Assignment operators

= stores a value. The compound forms update a variable using its current value:

OperatorSame as
x += 5x = x + 5
x -= 5x = x - 5
x *= 5x = x * 5
x /= 5x = x / 5
x %= 5x = x % 5
int score = 10;
score += 5;
score *= 2;
score -= 4;
score /= 3;
System.out.println(score);

Output:

8

Step 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.

OperatorMeaning
==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:

OperatorNameTrue when
&&ANDboth sides are true
||ORat least one side is true
!NOTflips 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
false

Short-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 yet

Because 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 ++x both add 1 to x.
  • x-- and --x both 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 6

On 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

What does this print?
int x = 3;
int y = x++ + ++x;
System.out.println(x + " " + y);

Operator precedence

When an expression has several operators, Java follows a fixed order, highest first:

OrderOperators
1() parentheses
2unary: ++ -- ! - (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
3345

The 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

What does this print?
boolean result = 5 > 3 || 10 / 2 == 4 && false;
System.out.println(result);

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 / 2 is 3. Write 7 / 2.0 or cast one side.
  • Using = instead of == in a condition.
  • Using == to compare Strings. Use .equals().
  • Forgetting precedence: (a + b) / 2 and a + b / 2 are different.
  • Writing & or | instead of && or ||. The single forms don't short-circuit.

Summary

  • / on two ints 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; ++x adds 1, then uses the new value.
  • Precedence: parentheses, unary, * / %, + -, comparisons, &&, ||, then assignment.

Next lesson: selection structures: making decisions with if, else if, and switch.

References and further reading