Skip to content
QuizMaker logoQuizMaker
Activity
Java Programming: From Zero to Enterprise
1. Java Fundamentals
1. Getting Started with Java & the JVM
2. Data Types & Variables
3. Control Flow: Ifs & Loops
4. String Manipulation in Depth
5. Methods (Functions) Architecture
6. Arrays & The Enhanced For Loop
7. User Input via Scanner
8. Mathematical Operations & The Math Class
9. Operators in Depth
10. Block Scope & Variable Lifecycles
11. Introduction to Object-Oriented Programming
12. Classes & Instances Deep Dive
13. Constructors
14. Encapsulation & The 'this' Keyword
15. Inheritance: Extending Functionality
16. Polymorphism & Method Overriding
17. Abstraction & Abstract Classes
18. Interfaces: The Ultimate Contract
19. Packages & Access Modifiers
20. Enums (Enumerations)
21. Exceptions: Handling Runtime Errors
22. The 'throw' and 'throws' keywords
23. Dates, Times, and Formatting
24. Enumerable Data Structures
25. LinkedLists: The Alternative
26. HashMaps: Key-Value Architecture
27. HashSets: The Art of Uniqueness
28. Iterator: Safe Collection Traversal
29. Wrapper Classes & Autoboxing
30. Basic File I/O
31. Generics: Type-Safe Templates
32. Lambda Expressions & Functional Interfaces
33. The Stream API: Functional Data Pipelines
34. Optional: Beating the NullPointerException
35. Multithreading & Concurrency Basics
36. JDBC: Connecting to SQL Databases
37. Annotations & Reflection
38. The JVM Garbage Collector
39. Introduction to Spring Boot
40. Unit Testing with JUnit
41. Java Collections for DSA
CONTENTS

9. Operators in Depth

Arithmetic, Assignment, Comparison, and Logical Operators.

Java Programming: From Zero to Enterprise
1. Java Fundamentals
February 22, 2026
118
A

[!NOTE] Operators are the symbols that perform operations on variables and values. You will need to chain these together frequently to create robust boolean conditions.

The 4 Categories of Operators

Category Purpose Examples
Arithmetic Math calculations +, -, *, /, % (Modulo)
Assignment Setting variable values =, +=, -=
Comparison Evaluating equality ==, !=, >, <=
Logical Chaining boolean logic && (AND), `

The Modulo Operator (%)

While / gives you the quotient of division, % gives you the Remainder of division. This is extremely useful for determining if a number is Even or Odd!

int remainder = 10 % 3; // Returns 1 (because 10 divided by 3 is 9, remainder 1).

if (number % 2 == 0) {
    System.out.println("This number is definitely EVEN.");
}

Logical Short-Circuit Evaluation

Logical operators (&& and ||) possess an intelligent "short-circuit" mechanism to speed up code execution and prevent Null Pointer Exceptions.

If the first condition in an && (AND) statement evaluates to false, Java realizes that the entire condition is doomed to fail. Therefore, it will not even bother evaluating the second condition.

Similarly, if the first condition in an || (OR) statement evaluates to true, Java will not evaluate the second condition, because the overall result is guaranteed to be true.

// Safe Execution Example!
// If 'user' is null, the first check fails. 
// Because of the && short-circuit, Java STOPS evaluating. 
// It never attempts to run user.isActive(), preventing an app crash!

if (user != null && user.isActive() == true) {
    System.out.println("User is logged in safely.");
}

[!CAUTION] If you actually want both sides to evaluate no matter what, use the bitwise & and | operators instead. But this is extremely rare in web applications.

Operators as Business Rules

Operators are small symbols, but they usually express important rules: whether a user can log in, whether a cart qualifies for free delivery, whether a number is in range, or whether a loop should continue.

Readable Boolean Logic

double cartTotal = 799.0;
boolean isMember = true;

boolean qualifiesByAmount = cartTotal >= 999.0;
boolean qualifiesByMembership = isMember && cartTotal >= 499.0;

if (qualifiesByAmount || qualifiesByMembership) {
    System.out.println("Free delivery applied");
}

Splitting the rule into named booleans makes the condition easier to understand and change later.

Integer Division Trap

int correct = 7;
int total = 10;

double wrongPercent = correct / total * 100;      // 0.0 because 7 / 10 is 0
double rightPercent = (double) correct / total * 100; // 70.0

When both operands are integers, Java performs integer division. Cast one value to double before dividing if you need a decimal result.

Common Mistakes

  • Confusing assignment = with comparison ==.
  • Forgetting operator precedence and relying on readers to guess.
  • Using & or | when short-circuiting && or || is intended.
  • Doing percentage math with integer division by accident.

Mini Practice

Create a login rule: user is allowed only when email is verified, password is correct, and account is not locked. Write it once as one large condition, then rewrite it using named boolean variables.

Practice Lab: Login Rule Builder

Use operators to express a real access rule.

  1. Create booleans for isEmailVerified, isPasswordCorrect, and isAccountLocked.
  2. Create a boolean called canLogin.
  3. Allow login only when email is verified, password is correct, and account is not locked.
  4. Print different messages for allowed and denied login.

Goal: Practice &&, ||, !, and readable boolean variables.

Revision Checkpoint

  • Assignment: = stores a value.
  • Comparison: ==, !=, >, and <= produce booleans.
  • Logical AND: && requires both sides to be true.
  • Logical OR: || requires at least one side to be true.
  • Modulo: % gives the remainder and is useful for even/odd checks.

Before the quiz: Explain why user != null && user.isActive() is safer than checking user.isActive() first.

Share this article

Share on TwitterShare on LinkedInShare on FacebookShare on WhatsAppShare on Email

Test your knowledge

Take a quick quiz based on this chapter.

mediumJava
Quiz: Operators
10 questions5 min
Lesson 9 of 10 in 1. Java Fundamentals
Previous in 1. Java Fundamentals
8. Mathematical Operations & The Math Class
Next in 1. Java Fundamentals
10. Block Scope & Variable Lifecycles
Back to Java Programming: From Zero to Enterprise
Back to moduleCategories