Java Operators ๐ฑ¶
Mentor's Note: Operators are the "Action Verbs" of programming. They tell the computer what to do with your data. Whether it's adding a score, checking a password, or repeating a taskโit all starts with an Operator! ๐ก
๐ The Scenario: The Smartphone Calculator ๐ฑ¶
Imagine you are building a simple calculator app.
- Arithmetic (The Math): You need buttons for Addition (+), Subtraction (-), and Multiplication (*). These do the heavy lifting of math. โ
- Comparison (The Decision): Your app needs to ask: "Is the user's input Equal to the correct answer?" (
==). โ - Logical (The Brain): Your app checks: "Is the battery above 5% AND is the screen on?" (
&&). ๐ง - The Result: A smart app that can calculate, compare, and make logical decisions. โ
๐จ Visual Logic: The Operator Toolkit¶
| Category | Role | Key Operators |
|---|---|---|
| Arithmetic | Math ๐งฎ | +, -, *, /, % |
| Comparison | Questions โ | ==, !=, >, <, >=, <= |
| Logical | Thinking ๐ง | && (AND), || (OR), ! (NOT) |
| Assignment | Storing ๐ฆ | =, +=, -=, *=, /= |
graph TD
A[Java Operators ๐ ๏ธ] --> B[Arithmetic: + - * / %]
A --> C[Comparison: == != > <]
A --> D[Logical: && || !]
A --> E[Assignment: = += -=]
๐ Concept Explanation¶
1. Arithmetic Operators ๐งฎ¶
Standard math operators. Note the Modulus (%) which returns the Remainder (e.g., 10 % 3 = 1).
2. Comparison Operators โ¶
These always return a boolean (true or false). They are the foundation of decision-making.
3. Logical Operators ๐ง ¶
Used to combine multiple comparisons.
- AND (&&): Both sides must be true.
- OR (||): At least one side must be true.
- NOT (!): Flips the result (True becomes False).
4. Increment & Decrement โฌ๏ธโฌ๏ธ¶
x++ is shorthand for x = x + 1. It is used millions of times in loops!
๐ป Implementation: The Calculator Lab¶
// ๐ Scenario: A mini-calculator check
// ๐ Action: Using different operators together
public class Main {
public static void main(String[] args) {
int a = 10, b = 3;
// ๐งฎ Arithmetic
System.out.println("Remainder (10 % 3): " + (a % b)); // Result: 1
// ๐ฆ Assignment Shorthand
int score = 50;
score += 10; // score = 60
// โ Comparison
boolean isGreater = (a > b); // true
// ๐ง Logical
boolean canVote = (a >= 18 && isGreater); // false
System.out.println("Can Vote: " + canVote);
}
}
๐ Sample Dry Run (Logic)¶
| Step | Instruction | Logic | Result |
|---|---|---|---|
| 1 | int x = 5; |
Store 5 | x = 5 ๐ฆ |
| 2 | x += 5; |
Add 5 to x | x = 10 โ |
| 3 | x > 15 |
Is 10 > 15? | false โ |
| 4 | !(x > 15) |
Flip 'false' | true โ
|
๐ Technical Analysis: Operator Precedence ๐ง ¶
Just like in school math (BODMAS/PEMDAS), Java follows a specific order.
1. Parentheses () always go first.
2. Multiplication/Division * / % come before Addition/Subtraction + -.
3. Comparison happens after Math.
4. Logical happens last.
๐ฏ Practice Lab ๐งช¶
Task: The Odd-Even Checker
Task: Write a program that takes a number and uses the Modulus (%) operator to check if it's even.
Goal: If number % 2 == 0, itโs even! ๐ก
๐ก Interview Tip ๐¶
"Interviewers love this: 'What is the difference between
==and.equals()?' Answer:==compares the Memory Address (useful for numbers), but.equals()compares the Actual Content (useful for Strings). Never use==for Strings!"
๐ก Pro Tip: "Increment (
++) and Decrement (--) are your best friends in loops. Master them early!" - Vishnu Damwala