Operators
Overview
Operators are special symbols used to perform operations on variables and values. They are the absolute foundation of all logic, mathematics, and decision-making in a Java application.
Java features several distinct categories of operators:
- Arithmetic Operators (+, -, *, /, %): Perform basic math. The modulo operator (%) is particularly useful for finding remainders and determining if numbers are even or odd.
- Assignment Operators (=, +=, -=): Assign or update values in a variable.
- Relational/Comparison Operators (==, !=, >, <, >=, <=): Compare two values, always resulting in a boolean (true or false). These are the core of if statements.
- Logical Operators (&&, ||, !): Combine multiple boolean conditions together. && means AND, || means OR.
- Unary Operators (++, --): Increment or decrement a number by exactly 1.
Syntax
The Modulo (%) operator is extremely powerful in algorithms. Also, notice that 10 / 3 results in 3, not 3.333, because dividing two integers in Java always produces an integer.
public class Main {
public static void main(String[] args) {
// --- Arithmetic ---
int a = 10, b = 3;
System.out.println(a / b); // Prints 3 (Integer division drops decimals!)
System.out.println(a % b); // Prints 1 (The remainder of 10 / 3)
// --- Unary (Increment) ---
int counter = 0;
counter++; // Increases to 1
// --- Relational & Logical ---
int age = 20;
boolean hasLicense = true;
// AND (&&) requires BOTH sides to be true
if (age >= 18 && hasLicense) {
System.out.println("You can drive!");
}
// OR (||) requires AT LEAST ONE side to be true
if (age < 18 || !hasLicense) {
System.out.println("You cannot drive.");
}
}
}Common Pitfalls
- Integer Division. If you divide
5 / 2, the result is2, NOT2.5. Because both operands are integers, Java chops off the decimal. To get accurate decimals, at least one number must be a double:5.0 / 2. - Using
==to compare Strings. The==operator checks if two objects point to the exact same memory address, not if they have the same text. ALWAYS usestr1.equals(str2)to compare Strings. - Pre vs Post Increment (
++xvsx++).int y = x++;assigns the current value of x to y, and THEN increments x.int y = ++x;increments x first, and THEN assigns it to y. This causes massive off-by-one errors in loops if misunderstood.
Interview Questions
When evaluating an AND (&&) expression, if the left side is false, Java instantly skips the right side because the entire statement is guaranteed to be false. Similarly, in an OR (||) expression, if the left side is true, Java skips the right side. This prevents NullPointerExceptions (e.g., if (obj != null && obj.isValid())).
=) and the Equality operator (==)?The = operator is used to ASSIGN a value to a variable (e.g., x = 5). The == operator is used to COMPARE two primitive values to see if they are identical (e.g., if (x == 5)).
Real-World Example
E-commerce checkout logic heavily relies on Logical Operators to validate state before processing payments.
public boolean canProcessCheckout(User user, Cart cart) {
// Short-circuiting prevents errors!
return user != null
&& user.isLoggedIn()
&& cart.getTotalItems() > 0
&& user.hasValidPayment();
}Check Your Knowledge
Test your understanding of Operators with these quick questions.