</>
Skip to content
Java lessons (10/47)

Java — Operators

Arithmetic operators

Java provides standard math operations for numeric types:

int a = 10;
int b = 3;

System.out.println(a + b);   // 13  — addition
System.out.println(a - b);   // 7   — subtraction
System.out.println(a * b);   // 30  — multiplication
System.out.println(a / b);   // 3   — integer division (truncated!)
System.out.println(a % b);   // 1   — modulus (remainder)

The integer division surprise: 10 / 3 gives 3, not 3.333.... Both operands are integers, so Java discards the decimal. To get a decimal result, at least one operand must be a floating-point type:

System.out.println(10.0 / 3);  // 3.3333333333333335
System.out.println((double) a / b);  // 3.3333333333333335

Modulus in practice

The remainder operator is more useful than it looks:

System.out.println(7 % 3);     // 1 — odd/even check
System.out.println(15 % 5);    // 0 — evenly divisible

// Practical: check if a number is even
int number = 42;
boolean isEven = number % 2 == 0;

Modulus wraps around — it's the foundation of circular buffers, clock arithmetic, and hash functions.

Increment and decrement

Java has shortcuts for adding or subtracting one:

int x = 5;
x++;        // x is now 6 (post-increment)
++x;        // x is now 7 (pre-increment)
x--;        // x is now 6 (post-decrement)
--x;        // x is now 5 (pre-decrement)

The difference matters when used inside an expression:

int a = 5;
int b = a++;   // b = 5, then a becomes 6
int c = ++a;   // a becomes 7, then c = 7

a++ returns the old value then increments. ++a increments first then returns the new value. For standalone statements, there's no difference — use whichever reads more clearly.

Assignment operators

Beyond basic assignment, Java has compound operators:

int x = 10;
x += 5;     // x = x + 5  → 15
x -= 3;     // x = x - 3  → 12
x *= 2;     // x = x * 2  → 24
x /= 4;     // x = x / 4  → 6
x %= 4;     // x = x % 4  → 2

These are shorthand — they don't perform differently, but they read better and prevent you from repeating the variable name.

Comparison operators

These return true or false:

int a = 10;
int b = 20;

System.out.println(a == b);   // false — equal to
System.out.println(a != b);   // true  — not equal to
System.out.println(a > b);    // false — greater than
System.out.println(a < b);    // true  — less than
System.out.println(a >= 10);  // true  — greater than or equal
System.out.println(a <= 5);   // false — less than or equal

Common mistake: = is assignment, == is comparison. Mixing them up is a classic Java error:

if (x = 5) { }   // ERROR: incompatible types
if (x == 5) { }  // correct

Java won't let you assign inside an if condition — it catches this mistake at compile time.

Logical operators

Combine boolean expressions:

boolean a = true;
boolean b = false;

System.out.println(a && b);   // false — AND: both must be true
System.out.println(a || b);   // true  — OR: at least one must be true
System.out.println(!a);       // false — NOT: flips the value

Real-world usage:

int age = 25;
boolean hasTicket = true;

if (age >= 18 && hasTicket) {
    System.out.println("You may enter.");
}

if (!hasTicket) {
    System.out.println("Please buy a ticket.");
}

Short-circuit evaluation

Java stops evaluating as soon as the result is determined:

if (x != 0 && 10 / x > 2) { ... }

If x is 0, the first condition is false. Java never evaluates 10 / x — avoiding a division-by-zero crash. This is short-circuit evaluation and it's a safety feature, not just an optimization.

For OR (||), if the first condition is true, the second is skipped:

if (list == null || list.isEmpty()) { ... }

Safe null check — list.isEmpty() only runs if list isn't null.

Bitwise operators

These operate on individual bits of integer values:

int a = 12;    // 1100 in binary
int b = 10;    // 1010 in binary

System.out.println(a & b);    // 8   — AND (1000)
System.out.println(a | b);    // 14  — OR  (1110)
System.out.println(a ^ b);    |   // 6   — XOR (0110)
System.out.println(~a);       // -13 — NOT (inverts all bits)
System.out.println(a << 2);   // 48  — left shift (multiply by 4)
System.out.println(a >> 1);   |   // 6   — right shift (divide by 2)

You won't use bitwise operators daily, but they appear in low-level code, cryptography, and flag-based permission systems.

The ternary operator

A compact if-else in one expression:

int age = 20;
String status = (age >= 18) ? "adult" : "minor";
System.out.println(status);  // adult

Format: condition ? valueIfTrue : valueIfFalse. Use it for simple decisions. For complex logic, a regular if-else reads better.

Operator precedence

When multiple operators appear in one expression, Java follows precedence rules:

int result = 2 + 3 * 4;    // 14, not 20 — multiplication first
int result2 = (2 + 3) * 4; // 20 — parentheses override precedence

Key precedence (highest to lowest):

  1. () — parentheses
  2. ++ -- ! — unary operators
  3. * / % — multiplicative
  4. + - — additive
  5. < > <= >= — relational
  6. == != — equality
  7. && || — logical
  8. = += *= — assignment

When in doubt, use parentheses. Explicit grouping prevents subtle precedence bugs.

Mini Practice

  1. Calculate the remainder of 17 / 5 and 17 % 5 — explain the difference
  2. Write a boolean expression that checks if a number is between 1 and 100
  3. Use the ternary operator to assign "even" or "odd" to a string based on a number
  4. Evaluate 3 + 4 * 5 and (3 + 4) * 5 — print both results
  5. Write an expression using && that checks if a number is positive AND less than 1000

Next: conditional statements — making decisions →

Related Topics

Frequently Asked Questions about Operators

What is Operators in Java?

Operators is a fundamental concept in Java. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Operators?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Operators.

Why is Operators important in Java?

Operators is essential for Java development. Understanding this concept will help you write better code and solve real-world problems more effectively.