Lesson 26 min read
Operators & Expressions
The math and logic behind every decision
Operators β Java's Toolbox
Operators are the verbs of your code. Variables hold the data, but operators do stuff with it β add, compare, combine, flip. Think of them like buttons on a calculator, except way more powerful. Mastering operators is your first step toward understanding algorithm complexity.
Java has several families of operators:
- Arithmetic β math stuff:
+-*/% - Relational β comparing things:
==!=<><=>= - Logical β combining true/false:
&&||! - Assignment β storing results:
=+=-=*= - Increment/Decrement β quick +1 or -1:
++--
Arithmetic & the Sneaky Integer Division
Relational & Logical Operators
Relational operators ask yes-or-no questions and give you back true or false. "Is 5 greater than 3?" β true.
Logical operators combine those answers:
&&(AND) β both sides must be true. Like needing both a ticket AND an ID to get in.||(OR) β at least one side must be true. Like a restaurant that accepts cash OR card.!(NOT) β flips true to false and vice versa. Like an "opposite day" button.
Comparisons & Logic in Action
Ternary Operator & Increment/Decrement
Note: Watch out for
== vs =! Single = means "put this value in the box." Double == means "are these two things equal?" Mixing them up is one of the most common beginner bugs. And remember: for Strings, always use .equals() instead of ==.Challenge