Skip to content

Operators – Doing Math and Comparisons in Java

Seanti edited this page Mar 24, 2025 · 1 revision

Operators are symbols that let us perform actions on values in Java, like adding numbers, comparing values, or checking conditions.

Think of operators as tools—each one helps us do something different with data!


Types of Operators in Java

Operator Type Think of it like... Used For Example
Arithmetic A calculator Doing math +, -, *, /, %
Comparison A weighing scale Checking if values are equal or different ==, !=, >, <, >=, <=
Logical A circuit switch Combining conditions (true/false) &&, `
Assignment A mailbox Storing values in variables =, +=, -=, *=, /=
Increment/Decrement A counter Adding or subtracting 1 ++, --

1. Arithmetic Operators (Doing Math)

These operators work just like a calculator!

Operator Action Example (a = 10, b = 5) Result
+ Addition a + b 15
- Subtraction a - b 5
* Multiplication a * b 50
/ Division a / b 2
% Modulus (Remainder) a % b 0 (remainder when dividing)

Example: Gumball Machine Math

public class GumballMath {
    public static void main(String[] args) {
        int gumballs = 10;
        int people = 3;
        
        System.out.println("Each person gets " + (gumballs / people) + " gumballs.");
        System.out.println("Leftover gumballs: " + (gumballs % people));
    }
}

Output:

Each person gets 3 gumballs.
Leftover gumballs: 1

Use arithmetic operators to do math in Java!


2. Comparison Operators (Checking Values)

These compare two values and return true or false.

Operator Action Example (a = 10, b = 5) Result
== Equal to a == b false
!= Not equal a != b true
> Greater than a > b true
< Less than a < b false
>= Greater or equal a >= 10 true
<= Less or equal b <= 5 true

Example: Who Has More Gumballs?

public class GumballComparison {
    public static void main(String[] args) {
        int myGumballs = 7;
        int yourGumballs = 10;

        if (myGumballs > yourGumballs) {
            System.out.println("I have more gumballs!");
        } else {
            System.out.println("You have more gumballs!");
        }
    }
}

Output:

You have more gumballs!

Use comparison operators to check if values are the same or different!


3. Logical Operators (Combining Conditions)

These combine multiple conditions together.

Operator Action Example (a = 10, b = 5, c = 7) Result
&& AND (Both must be true) (a > b && a > c) true
` ` OR (At least one must be true)
! NOT (Flips true to false) !(a > b) false

Example: Can You Buy Gumballs?

public class GumballLogic {
    public static void main(String[] args) {
        int money = 5;
        boolean hasCoupon = true;

        if (money >= 3 || hasCoupon) {
            System.out.println("You can buy gumballs!");
        } else {
            System.out.println("You need more money or a coupon.");
        }
    }
}

Output:

You can buy gumballs!

Use logical operators to combine multiple conditions!


4. Assignment Operators (Storing Values)

These store values in variables.

Operator Example (x = 10) Same as... Result
= x = 5 x = 5 x is now 5
+= x += 3 x = x + 3 x is now 13
-= x -= 2 x = x - 2 x is now 8
*= x *= 2 x = x * 2 x is now 20
/= x /= 5 x = x / 5 x is now 2

Example: Saving Gumballs Over Time

public class GumballSaver {
    public static void main(String[] args) {
        int gumballs = 10;

        gumballs += 5; // Bought 5 more
        System.out.println("Now I have " + gumballs + " gumballs.");

        gumballs -= 3; // Ate 3
        System.out.println("After eating, " + gumballs + " left.");
    }
}

Output:

Now I have 15 gumballs.
After eating, 12 left.

Use assignment operators to quickly update variables!


5. Increment & Decrement Operators (Counting Up and Down)

These increase or decrease a value by 1.

Operator Example (x = 5) Same as... Result
++ x++ x = x + 1 6
-- x-- x = x - 1 4

Example: Counting Gumballs

public class GumballCounter {
    public static void main(String[] args) {
        int gumballs = 3;
        
        gumballs++; // Adds 1
        System.out.println("Now I have " + gumballs + " gumballs.");

        gumballs--; // Subtracts 1
        System.out.println("Now I have " + gumballs + " gumballs again.");
    }
}

Output:

Now I have 4 gumballs.
Now I have 3 gumballs again.

Use ++ and -- to quickly count up or down!


Final Takeaway: What Operators Do

Operator Type Used For
Arithmetic Doing math (+, -, *, /, %)
Comparison Checking values (==, !=, >, <, >=, <=)
Logical Combining conditions (&&, `
Assignment Storing values (=, +=, -=, *=, /=)
Increment/Decrement Counting up/down (++, --)

That’s it for operators! 🎉

Clone this wiki locally