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

public class Main {
public static void main(String[] args) {
int a = 17;
int b = 5;
System.out.println("Add: " + (a + b)); // 22
System.out.println("Subtract: " + (a - b)); // 12
System.out.println("Multiply: " + (a * b)); // 85
System.out.println("Divide: " + (a / b)); // 3, NOT 3.4!
System.out.println("Remainder: " + (a % b)); // 2
// To get the real decimal answer, make one side a double
System.out.println("Real divide: " + ((double) a / b)); // 3.4
}
}
Output
Add: 22
Subtract: 12
Multiply: 85
Divide: 3
Remainder: 2
Real divide: 3.4

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

public class Main {
public static void main(String[] args) {
int age = 15;
boolean hasPermission = true;
// Relational operators
System.out.println("Is teenager? " + (age >= 13 && age <= 19));
System.out.println("Is adult? " + (age >= 18));
// Logical operators
boolean canWatch = age >= 13 || hasPermission;
System.out.println("Can watch PG-13? " + canWatch);
// NOT operator
boolean isSleeping = false;
System.out.println("Is awake? " + !isSleeping);
// Combining multiple conditions
boolean canDrive = age >= 16 && hasPermission && !isSleeping;
System.out.println("Can drive? " + canDrive);
}
}
Output
Is teenager? true
Is adult? false
Can watch PG-13? true
Is awake? true
Can drive? false

Ternary Operator & Increment/Decrement

public class Main {
public static void main(String[] args) {
// Ternary: condition ? valueIfTrue : valueIfFalse
int score = 85;
String result = score >= 60 ? "Pass" : "Fail";
System.out.println("Result: " + result);
// It's a one-line if/else!
int temp = 30;
String weather = temp > 25 ? "Hot" : "Cool";
System.out.println("Weather: " + weather);
// Increment & Decrement
int lives = 3;
System.out.println("Lives: " + lives);
lives--; // lost a life
System.out.println("After hit: " + lives);
lives++; // found a 1-up!
System.out.println("Found 1-up: " + lives);
// Pre vs Post increment
int x = 5;
System.out.println("Post: " + x++); // prints 5, THEN adds 1
System.out.println("Now x is: " + x); // 6
System.out.println("Pre: " + ++x); // adds 1 FIRST, then prints 7
}
}
Output
Result: Pass
Weather: Hot
Lives: 3
After hit: 2
Found 1-up: 3
Post: 5
Now x is: 6
Pre: 7
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 ==.

Quick check

What does 17 / 5 give you in Java (both are ints)?
Variables & Data TypesStrings