Java Operators: Compute Values with Confidence

Introduction

In this chapter, you will learn Java operators—symbols that perform calculations, comparisons, and logical combinations. Operators turn raw values into useful results for decisions and programs. They build directly on Variables and prepare you for conditionals.

Prerequisites

  • JDK 21 and a working main class
  • Basic understanding of Variables
  • Ability to run code in IntelliJ or the terminal

What Is an Operator

An operator tells Java what action to perform on values (operands).

Simple metaphor:

  • Values are ingredients
  • Operators are actions (+ combine, * scale, == compare)
  • The expression’s result is the dish
java
int total = 3 + 5;
System.out.println(total);  // 8

1) Arithmetic Operators

OperatorMeaningExample (int a = 10, b = 3)
+Additiona + b → 13
-Subtractiona - b → 7
*Multiplicationa * b → 30
/Divisiona / b → 3 (integer division)
%Remainder (modulo)a % b → 1
java
public class ArithmeticDemo {
    public static void main(String[] args) {
        int a = 10;
        int b = 3;
 
        System.out.println(a + b);  // 13
        System.out.println(a - b);  // 7
        System.out.println(a * b);  // 30
        System.out.println(a / b);  // 3  (not 3.333...)
        System.out.println(a % b);  // 1
    }
}

Tip

Practical Hint

Use n % 2 == 0 to test even numbers. Use % for wrap-around patterns such as clock arithmetic.

Integer vs Floating-Point Division

If either operand is double (or float), / performs floating-point division:

java
System.out.println(10 / 3);      // 3
System.out.println(10.0 / 3);    // 3.333...
System.out.println((double) 10 / 3);  // 3.333...

Exponent

Java has no ** operator. Use Math.pow(base, exponent) (returns double):

java
double power = Math.pow(2, 10);  // 1024.0

2) Assignment Operators

= assigns a value. Compound operators update and assign in one step.

OperatorExampleSame as
=x = 5assign
+=x += 2x = x + 2
-=x -= 2x = x - 2
*=x *= 2x = x * 2
/=x /= 2x = x / 2
%=x %= 2x = x % 2
java
int score = 50;
score += 10;   // 60
score -= 5;    // 55
score *= 2;    // 110
System.out.println(score);

3) Increment and Decrement (Unary)

++ and -- add or subtract 1. Used often in loops (later chapter).

java
int count = 0;
count++;   // count is 1
count--;   // count is 0 again

Prefix (++i) vs postfix (i++) differ in expressions that use the value in the same statement. For now, using count += 1 is clearer until you are comfortable with loops.

4) Comparison Operators

Comparisons produce boolean results (true or false).

OperatorMeaning
==equal
!=not equal
>greater than
<less than
>=greater than or equal
<=less than or equal
java
int x = 8;
int y = 12;
 
System.out.println(x == y);  // false
System.out.println(x != y);  // true
System.out.println(x < y);   // true
System.out.println(x >= y);  // false

Warning

Do not confuse = and ==.
= assigns; == compares primitive values.

For String text, use .equals() instead of == in most cases—covered in Strings.

5) Logical Operators

Combine boolean conditions.

OperatorMeaning
&&logical AND (both true)
||logical OR (at least one true)
!logical NOT (invert)
java
int age = 20;
boolean hasTicket = true;
 
boolean canEnter = age >= 18 && hasTicket;
System.out.println(canEnter);  // true

Java uses short-circuit evaluation: in a && b, if a is false, b is not evaluated; in a || b, if a is true, b is not evaluated. This matters when the right side has side effects—advanced topic for now, but good to know the name.

6) Operator Precedence (Execution Order)

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

Simplified order (high to low):

  1. Parentheses ( )
  2. Unary ++, --, !
  3. Multiplicative *, /, %
  4. Additive +, -
  5. Relational <, <=, >, >=
  6. Equality ==, !=
  7. Logical &&
  8. Logical ||
  9. Assignment =, +=, …
java
int result1 = 2 + 3 * 4;      // 14 (multiply first)
int result2 = (2 + 3) * 4;    // 20 (parentheses first)
 
System.out.println(result1);
System.out.println(result2);

When in doubt, use parentheses to show intent. Clear code beats memorizing every precedence level.

Mini Practice

java
public class OperatorPractice {
    public static void main(String[] args) {
        int num1 = 15;
        int num2 = 4;
 
        System.out.println("sum: " + (num1 + num2));
        System.out.println("difference: " + (num1 - num2));
        System.out.println("product: " + (num1 * num2));
        System.out.println("division: " + (num1 / num2));
        System.out.println("remainder: " + (num1 % num2));
 
        System.out.println("num1 > num2: " + (num1 > num2));
        System.out.println("both positive: " + (num1 > 0 && num2 > 0));
    }
}

Common Beginner Mistakes

Mistake 1: Integer Division Surprise

10 / 3 is 3, not 3.33. Cast or use double when you need decimals.

Mistake 2: Forgetting Parentheses

2 + 3 * 4 is not 20. Parentheses document your intent.

Mistake 3: Using = in a Condition

java
// if (age = 18) { }  // Compile error in Java — good!
if (age == 18) { }

Java prevents many accidental assignments in conditions; still be deliberate with ==.

What’s Next

Deepen numeric types in Numbers, then build your first interactive program in Sum of Two Numbers.

FAQ

Which operators should I memorize first?

Arithmetic (+ - * / %), comparison (== != > <), and logical (&& || !). That covers most beginner tasks.

Why is 10 / 4 equal to 2?

Both operands are int, so Java performs integer division and truncates toward zero.

When should I use %?

When you need remainders: even/odd checks, cycling indexes, or “every Nth item” logic.

Does Java have and / or keywords like Python?

No. Java uses && and || for boolean logic.

Is operator precedence important for beginners?

Yes. Use parentheses until precedence feels natural.