Basic Operators
Arithmetic operators are the foundation of all numeric calculations in Java.
They are used to perform simple math operations such as addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).
These operators work with all primitive numeric types like int, float, and double, and they follow the same rules you know from mathematics.
In real-world web development, arithmetic operators are used for far more than just textbook calculations.
They help manage counters in loops, calculate totals in e-commerce shopping carts, process user input for billing or scores, and implement algorithms for things like averages or percentages.
Itโs also important to remember that the division operator (/) behaves differently for integers and decimals.
When you divide two integers, the result is an integer โ any fraction is discarded.
To get an accurate decimal result, at least one operand must be a float or double.
The modulus operator (%) is especially useful for tasks like checking if a number is even or odd, rotating through indexes in a list, or creating repeating patterns.
Practicing with arithmetic operators builds a solid foundation for bigger calculations that web applications handle behind the scenes.
Mini Lab
Comparison operators allow your programs to make decisions by comparing two values.
They check relationships between values and return either true or false.
Javaโs comparison operators include equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=).
These operators are essential for building conditions in if statements, loops, and many algorithms.
They let you verify user input, check for valid data ranges, control when a loop should stop, or decide whether certain logic should run.
One subtle but important point is that comparison operators always produce a boolean result โ true or false.
Combining them with logical operators allows you to build complex multi-step conditions.
Another best practice is to be careful with == when comparing objects.
Primitive types like int or double compare by value, but objects like String should be compared using .equals()
instead of == to avoid unexpected behavior.
In web development, comparison operators help you validate form fields, check login credentials, compare dates, prices, or scores, and handle branching logic for different user scenarios.
Mini Lab
Logical operators allow you to combine multiple boolean expressions to form more advanced conditions.
Java provides three main logical operators: AND (&&), OR (||), and NOT (!).
They help your programs make more intelligent decisions by testing whether multiple conditions are true or false together.
The AND operator (&&) returns true only if both conditions are true.
OR (||) returns true if at least one condition is true.
The NOT operator (!) flips a boolean value โ true becomes false, and false becomes true.
Logical operators are essential in if statements and loops that depend on more than one factor.
For example, you might check whether a user is logged in and has permission to access a page.
Or you might accept an input only if it is within a range or matches a special override code.
In web development, logical operators handle scenarios like checking multiple form fields, validating combined user input, or defining complex business rules that must be true together.
Combining logical and comparison operators gives you complete control over how your program behaves under different conditions.
Mini Lab