Aral Code

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

OperatorMeaningExampleResult
+Add10 + 313
-Subtract10 - 37
*Multiply10 * 330
/Divide10 / 33 (int division)
%Remainder10 % 31

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

OperatorSame as
x += 5x = x + 5
x -= 5x = x - 5
x *= 5x = x * 5
x /= 5x = x / 5
x %= 5x = 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:

OperatorMeaning
==Equal to
!=Not equal to
> / <Greater / less than
>= / <=Greater / less than or equal

Logical operators

OperatorMeaningTrue when…
&&ANDboth are true
||ORat least one is true
!NOTthe 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:

  1. Parentheses ( )
  2. ++, --, !
  3. *, /, %
  4. +, -
  5. <, >, <=, >=
  6. ==, !=
  7. &&
  8. ||
  9. =, +=, -= …

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

1. What is 17 % 4?
2. After int a = 3; int b = a++;, what is b?
3. Which operator means AND?
4. How do you correctly compare two Strings?

By Sir Reyel · Last updated 2026-12-04