Java ยท Chapter 9 of 42

Java Operators

Operators perform operations on variables and values. Java supports arithmetic (+, -, *, /, %), assignment (=, +=, -=), comparison (==, !=, <, >), and logical (&&, ||, !) operators.

The % (modulus) operator returns the remainder of division, and is very useful for tasks like checking even/odd numbers.

Syntax
a + b
a == b
a && b

Arithmetic and assignment

Basic math operators combine with = to form compound assignment operators like +=, -=, *=, /= for shorthand updates.

Comparison and logical

Comparison operators return a boolean. Logical operators && (AND), || (OR) and ! (NOT) combine boolean expressions.

Example 1 (java)
public class Main {
  public static void main(String[] args) {
    int a = 10, b = 3;
    System.out.println(a + b);
    System.out.println(a % b);
    System.out.println(a > b && b > 0);
  }
}
Output
13
1
true

Arithmetic, modulus, and a logical AND expression are evaluated.

Key points

  • % returns the remainder of division.
  • Comparison operators return boolean values.
  • && is AND, || is OR, ! is NOT.
  • Compound assignment like += shortens common updates.
๐Ÿ’ก Note: Java uses short-circuit evaluation for && and ||, so the second operand may not be evaluated.

๐Ÿ“ Quick Quiz

1. What does the % operator return?

2. Which operator represents logical AND?

3. What type does a comparison operator return?