☕ Java
Operators in Java
Operators are the symbols that tell Java what to do with your data — add it, compare it, flip it, combine it. You use operators in literally every line of real code, so getting comfortable with them early pays off immediately.
Arithmetic Operators — Math in Code
The basics — but with one important gotcha: integer division in Java truncates, it doesn't round. 10 / 3 gives you 3, not 3.33. If you want the decimal, make sure at least one operand is a double.
Java
int a = 10, b = 3;
System.out.println(a + b); // 13 — addition
System.out.println(a - b); // 7 — subtraction
System.out.println(a * b); // 30 — multiplication
System.out.println(a / b); // 3 — integer division (3.33... gets chopped to 3!)
System.out.println(a % b); // 1 — modulus (remainder) — great for checking even/odd
System.out.println(a++); // 10 — post-increment: uses current value, THEN increments
System.out.println(++a); // 12 — pre-increment: increments FIRST, then uses value
// Fix integer division: cast to double first
System.out.println((double) a / b); // 3.333...Relational & Logical Operators — Decision Making
These operators produce a boolean (true/false) result. They're the backbone of every if statement, every loop condition, every filter.
Java
int x = 5, y = 10;
// Relational — comparing two values
System.out.println(x > y); // false — is 5 greater than 10? No.
System.out.println(x < y); // true
System.out.println(x == y); // false — equality check (NOT assignment, that's =)
System.out.println(x != y); // true — not equal
// Logical — combining conditions
// Real use: "Is the user logged in AND is their account active?"
System.out.println(x < y && x > 0); // true — both must be true (AND)
System.out.println(x > y || x > 0); // true — at least one must be true (OR)
System.out.println(!(x > y)); // true — flips the result (NOT)Ternary Operator — One-Line if/else
The ternary operator is a compact way to assign a value based on a condition. Perfect for simple yes/no decisions. Think of it as: "If this is true, give me this — otherwise give me that."
Java
int age = 20;
String status = (age >= 18) ? "Adult" : "Minor";
System.out.println(status); // Adult
// Real-world usage: showing discount label
double cartTotal = 1500.0;
String discount = (cartTotal > 1000) ? "10% OFF applied" : "No discount";
// Nested ternary (use sparingly — readability drops fast)
int score = 72;
String grade = (score >= 90) ? "A" : (score >= 75) ? "B" : (score >= 60) ? "C" : "F";Related Topics in Java Basics
Variables in Java
A variable is just a named box in memory that holds a value. Java is strict about what goes in each box — you tell it the type upfront. Once you get this, the rest of Java clicks into place.
Data Types in Java
Java needs to know exactly what kind of data it's dealing with before it can store or process it. Integers, decimals, characters, true/false — each has its own type. Knowing which to use (and why) makes your programs efficient and bug-free.
Control Flow Statements
Without control flow, every program would just run from top to bottom in a straight line. Control flow lets your program make decisions, repeat actions, and skip code — the difference between a calculator and an actual application.
Arrays in Java
An array is how you store a list of values under one name. Instead of creating 100 separate variables for 100 exam scores, you create one array. Simple idea, but it unlocks a ton of power.