-
Notifications
You must be signed in to change notification settings - Fork 0
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!
| 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 |
++, --
|
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) |
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));
}
}Each person gets 3 gumballs.
Leftover gumballs: 1
✅ Use arithmetic operators to do math in Java!
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 |
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!");
}
}
}You have more gumballs!
✅ Use comparison operators to check if values are the same or different!
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 |
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.");
}
}
}You can buy gumballs!
✅ Use logical operators to combine multiple conditions!
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
|
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.");
}
}Now I have 15 gumballs.
After eating, 12 left.
✅ Use assignment operators to quickly update variables!
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 |
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.");
}
}Now I have 4 gumballs.
Now I have 3 gumballs again.
✅ Use ++ and -- to quickly count up or down!
| 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! 🎉