Java Operators

Operators are the symbols that let your programs actually do something with values: add them, compare them, combine conditions, and store results. Java groups its operators into a handful of families, and this page gives you the big picture before you dive into each one.

What are operators?

An operator performs an action on one or more values, which we call operands. In the expression 5 + 3, the plus sign is the operator and 5 and 3 are the operands. Java offers several categories of operators, each suited to a different kind of job.

  • Arithmetic operators do maths: + - * / and %.
  • Assignment operators store or update values: = += -= and friends.
  • Comparison operators test how two values relate: == != > < >= and <=.
  • Logical operators combine boolean conditions: && || and !.

A quick taste of each family

The example below touches every category at once so you can see how they fit together in a normal program. Read it top to bottom and notice how the result of one operation feeds into the next.

Operators working together

public class Main {
  public static void main(String[] args) {
    int a = 10, b = 3;

    int sum = a + b;            // arithmetic -> 13
    boolean bigger = a > b;     // comparison -> true
    boolean both = bigger && (sum > 5); // logical -> true
    sum += 100;                 // assignment -> sum is now 113

    System.out.println(sum);    // 113
    System.out.println(both);   // true
  }
}

Operator categories at a glance

CategoryExample operatorsTypical result type
Arithmetic+ - * / %a number
Assignment= += -= *=stores a value
Comparison== != > < >= <=a boolean
Logical&& || !a boolean

Precedence: who goes first?

Just like in school maths, Java evaluates some operators before others. Multiplication and division happen before addition and subtraction, and you can always use parentheses to make the order explicit. When in doubt, add brackets; they cost nothing and make your intent obvious.

Precedence in action

public class Main {
  public static void main(String[] args) {
    int result1 = 2 + 3 * 4;   // 14, because * runs before +
    int result2 = (2 + 3) * 4; // 20, brackets force + first

    System.out.println(result1);
    System.out.println(result2);
  }
}
Note: You do not need to memorise the full precedence table. Reach for parentheses whenever an expression mixes several operators - it prevents bugs and makes your code easier to read.

The following pages break each family down in detail, with tables and runnable examples. Master these four groups and you can express almost any calculation or decision your programs need to make.

Exercise: Java Operators

What does the % operator return when applied to two integers?