Operators are the backbone of logic in Java. They allow you to perform calculations, make decisions, compare values, and manipulate data.
If variables are the building blocks, operators are the tools that make them useful.
🔹 What are Operators in Java?
An operator is a symbol that performs an operation on one or more operands (values or variables).
👉 Example:
int a = 10;
int b = 5;
int sum = a + b;
👉 Here:
-
+is the operator -
aandbare operands
🔹 Types of Operators in Java
Java operators are divided into several categories:
- Arithmetic Operators
- Relational (Comparison) Operators
- Logical Operators
- Assignment Operators
- Unary Operators
- Bitwise Operators
- Ternary Operator
Let’s understand each with examples.
🔹 1. Arithmetic Operators
Used to perform basic mathematical operations.
| Operator | Meaning |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus (remainder) |
🔸 Example:
public class ArithmeticExample {
public static void main(String[] args) {
int a = 10, b = 3;
System.out.println(a + b); // 13
System.out.println(a - b); // 7
System.out.println(a * b); // 30
System.out.println(a / b); // 3
System.out.println(a % b); // 1
}
}
👉 Note: Integer division removes decimal values.
🔹 2. Relational (Comparison) Operators
Used to compare two values and return true or false.
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal |
| <= | Less than or equal |
🔸 Example:
int a = 10, b = 20;
System.out.println(a == b); // false
System.out.println(a != b); // true
System.out.println(a > b); // false
System.out.println(a < b); // true
👉 These are commonly used in conditions.
🔹 3. Logical Operators
Used to combine multiple conditions.
| Operator | Meaning |
|---|---|
| && | AND |
| ! | NOT |
🔸 Example:
int age = 25;
System.out.println(age > 18 && age < 60); // true
System.out.println(age < 18 || age > 60); // false
System.out.println(!(age > 18)); // false
👉 Used in decision-making (if, loops).
🔹 4. Assignment Operators
Used to assign values to variables.
| Operator | Meaning |
|---|---|
| = | Assign |
| += | Add and assign |
| -= | Subtract and assign |
| *= | Multiply and assign |
| /= | Divide and assign |
🔸 Example:
int x = 10;
x += 5; // x = x + 5 → 15
x -= 3; // x = x - 3 → 12
x *= 2; // x = x * 2 → 24
x /= 4; // x = x / 4 → 6
👉 Helps write shorter and cleaner code.
🔹 5. Unary Operators
Operate on a single operand.
| Operator | Meaning |
|---|---|
| + | Positive |
| - | Negative |
| ++ | Increment |
| -- | Decrement |
🔸 Example:
int a = 5;
System.out.println(++a); // 6 (pre-increment)
System.out.println(a++); // 6 (post-increment)
System.out.println(a); // 7
👉 Important in loops.
🔹 6. Bitwise Operators
Used to perform operations at the bit level.
| Operator | Meaning |
|---|---|
| & | AND |
| ^ | XOR |
| ~ | NOT |
| << | Left shift |
| >> | Right shift |
🔸 Example:
int a = 5; // 0101
int b = 3; // 0011
System.out.println(a & b); // 1
System.out.println(a | b); // 7
System.out.println(a ^ b); // 6
👉 Mostly used in low-level or performance-critical code.
🔹 7. Ternary Operator
A shortcut for if-else.
🔸 Syntax:
condition ? value1 : value2;
🔸 Example:
int age = 20;
String result = (age >= 18) ? "Adult" : "Minor";
System.out.println(result);
👉 Makes code concise and readable.
🔹 Operator Precedence (Important)
When multiple operators are used, Java follows precedence rules.
👉 Example:
int result = 10 + 5 * 2;
👉 Output:
20
Because:
-
*executes before+
🔹 Real-Life Example
int marks = 85;
if (marks >= 50 && marks <= 100) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
👉 Combines:
- Relational operators
- Logical operators
🔹 Common Beginner Mistakes
❌ Confusing = with ==
❌ Misusing ++ (pre vs post)
❌ Ignoring operator precedence
❌ Overcomplicating conditions
👉 These lead to logical errors.
🔹 Best Practices
✔ Use parentheses for clarity
✔ Keep conditions simple
✔ Avoid complex nested expressions
✔ Write readable code
🔹 What to Learn Next
After operators, move to:
- Control Statements (if, switch)
- Loops (for, while)
- Arrays & Strings
👉 This will help you build logic.
🔹 Conclusion
Operators are essential for:
- Performing calculations
- Making decisions
- Controlling program flow
👉 Mastering operators means you can start building real logic in Java.
Remember:
Logic + Operators = Real Programming
Comments
Post a Comment