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.

Feb 22, 202635 views0 likes0 fires
18px

[!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.

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
9 questions5 min

Continue Learning

10. Block Scope & Variable Lifecycles

Beginner
8 min

11. Introduction to Object-Oriented Programming

Beginner
10 min

12. Classes & Instances Deep Dive

Beginner
12 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 Java Programming: From Zero to EnterpriseAll Categories