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 ==.