Monday, July 21, 2025

Operator in Java

In Java, **operators** are special symbols or keywords used to perform operations on variables and values. They are the building blocks of expressions and enable computations, comparisons, and logical decisions. Here’s a comprehensive breakdown of Java operators:

---

### **1. Types of Operators in Java**
#### **A. Arithmetic Operators**  
Used for mathematical calculations:
- `+` → Addition (e.g., `5 + 3 = 8`).
- `-` → Subtraction (e.g., `5 - 3 = 2`).
- `*` → Multiplication (e.g., `5 * 3 = 15`).
- `/` → Division (e.g., `5 / 3 = 1` for integers).
- `%` → Modulus (remainder, e.g., `5 % 3 = 2`).
- `++` → Increment (e.g., `i++`).
- `--` → Decrement (e.g., `i--`).

**Example:**
```java
int a = 10 + 5;  // Result: 15
```

---

#### **B. Relational Operators**  
Compare values and return `boolean` (`true`/`false`):
- `==` → Equal to (e.g., `5 == 3` → `false`).
- `!=` → Not equal to (e.g., `5 != 3` → `true`).
- `>` → Greater than (e.g., `5 > 3` → `true`).
- `<` → Less than (e.g., `5 < 3` → `false`).
- `>=` → Greater than or equal to.
- `<=` → Less than or equal to.

**Example:**
```java
boolean result = (10 > 5);  // Result: true
```

---

#### **C. Logical Operators**  
Used with boolean expressions:
- `&&` → Logical AND (e.g., `true && false` → `false`).
- `||` → Logical OR (e.g., `true || false` → `true`).
- `!` → Logical NOT (e.g., `!true` → `false`).

**Example:**
```java
boolean res = (5 > 3) && (2 < 4);  // Result: true
```

---

#### **D. Bitwise Operators**  
Work on individual bits of integers:
- `&` → Bitwise AND.
- `|` → Bitwise OR.
- `^` → Bitwise XOR.
- `~` → Bitwise complement (inverts bits).
- `<<` → Left shift (e.g., `5 << 1` → `10`).
- `>>` → Right shift (e.g., `5 >> 1` → `2`).

**Example:**
```java
int x = 5 & 3;  // Binary: 101 & 011 = 001 → Result: 1
```

---

#### **E. Assignment Operators**  
Assign values to variables:
- `=` → Simple assignment (e.g., `int a = 5`).
- `+=` → Add and assign (e.g., `a += 3` → `a = a + 3`).
- `-=`, `*=`, `/=` → Similar compound operations.

**Example:**
```java
int b = 10;
b *= 2;  // Result: 20
```

---

#### **F. Ternary Operator (`? :`)**  
Shortcut for `if-else`:
```java
int min = (a < b) ? a : b;  // Returns the smaller value
```

---

#### **G. Instanceof Operator**  
Checks object type:
```java
String s = "Hello";
boolean check = s instanceof String;  // Result: true
```

---

### **2. Operator Precedence**  
Order in which operators are evaluated (e.g., `*` before `+`). Use parentheses `()` to override:
```java
int result = 5 + 2 * 3;  // 2*3=6 → 5+6=11
```

---

### **3. Common Pitfalls**
- **Division with Integers**: `5 / 2` → `2` (use `5.0 / 2` for `2.5`).
- **Floating-Precision Errors**: `0.1 + 0.2 != 0.3` (due to binary floating-point representation).
- **Short-Circuiting**: `&&` and `||` stop evaluating if the result is determined early.

---

### **Example: Combining Operators**
```java
int a = 10, b = 5;
boolean res = (a > b) && ((a + b) == 15);  // true && true → true
```

---

### **Key Takeaways**
- Operators perform actions on operands (variables/values).
- Java supports arithmetic, logical, bitwise, and relational operators.
- Precedence rules dictate evaluation order.

Need help with a specific operator or scenario? Feel free to ask! 😊

No comments: