🧠 Java Operators — Full Explanation (Telugu + English)
🔹 Definition / నిర్వచనం
Operators అంటే మనం variables మరియు values పై operations చేయడానికి ఉపయోగించే చిహ్నాలు లేదా symbols.
In Java, operators are special symbols that perform operations on variables and values.
⚙️ Types of Operators in Java
Javaలో మొత్తం 7 main types of operators ఉంటాయి:
| Type | Example | Meaning |
|---|---|---|
| 1️⃣ Arithmetic Operators | +, -, *, /, % | గణిత క్రియలు చేయడానికి |
| 2️⃣ Unary Operators | ++, --, +, - | Single operand మీద పనిచేస్తాయి |
| 3️⃣ Assignment Operators | =, +=, -=, *=, /=, %= | విలువలు assign చేయడానికి |
| 4️⃣ Relational Operators | ==, !=, >, <, >=, <= | Compare చేయడానికి |
| 5️⃣ Logical Operators | &&, ||, ! | Logic conditions check చేయడానికి |
| 6️⃣ Bitwise Operators | &, |, ^, ~, <<, >>, >>> | Bits పై పనిచేస్తాయి |
| 7️⃣ Ternary Operator | ? : | Short if-else condition |
| 8️⃣ Type Cast Operator | (datatype) | Type convert చేయడానికి |
🧮 1️⃣ Arithmetic Operators
| Operator | Example | Result | Explanation |
|---|---|---|---|
| + | 10 + 5 | 15 | Addition |
| - | 10 - 5 | 5 | Subtraction |
| * | 10 * 5 | 50 | Multiplication |
| / | 10 / 5 | 2 | Division |
| % | 10 % 3 | 1 | Remainder |
🧾 Example:
🔁 2️⃣ Unary Operators
| Operator | Description | Example | Result |
|---|---|---|---|
| ++ | Increment | ++a | a = a + 1 |
| -- | Decrement | --a | a = a - 1 |
| + | Positive sign | +a | No change |
| - | Negative sign | -a | Changes sign |
🧾 Example:
📦 3️⃣ Assignment Operators
| Operator | Example | Equivalent To |
|---|---|---|
| = | a = 10 | a = 10 |
| += | a += 5 | a = a + 5 |
| -= | a -= 5 | a = a - 5 |
| *= | a *= 5 | a = a * 5 |
| /= | a /= 5 | a = a / 5 |
| %= | a %= 5 | a = a % 5 |
🧾 Example:
⚖️ 4️⃣ Relational (Comparison) Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 10 == 10 | true |
| != | Not equal to | 10 != 5 | true |
| > | Greater than | 10 > 5 | true |
| < | Less than | 10 < 5 | false |
| >= | Greater or equal | 10 >= 10 | true |
| <= | Less or equal | 5 <= 10 | true |
🧾 Example:
🔍 5️⃣ Logical Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
| && | Logical AND | (x>5 && y<10) | true if both true |
| || | Logical OR | (x>5 || y<10) | true if any one true |
| ! | Logical NOT | !(x>5) | opposite value |
🧾 Example:
⚡ 6️⃣ Bitwise Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
| & | AND | 5 & 3 | 1 |
| | | OR | 5 | 3 | 7 |
| ^ | XOR | 5 ^ 3 | 6 |
| ~ | NOT | ~5 | -6 |
| << | Left shift | 5 << 1 | 10 |
| >> | Right shift | 5 >> 1 | 2 |
| >>> | Unsigned right shift | -5 >>> 1 | large positive value |
❓ 7️⃣ Ternary Operator
Syntax:
🧾 Example:
Output:
🔄 8️⃣ Type Cast Operator
Used to convert one data type to another.
🧾 Example:
🧠 Small Interview Questions
1️⃣ What is the difference between == and =?
➡️ == is comparison operator, = is assignment operator.
2️⃣ What is the difference between && and &?
➡️ && is logical AND (used with boolean), & is bitwise AND (used with bits).
3️⃣ What is the output of 5 + 2 * 3?
➡️ 11 (Multiplication has higher precedence)
4️⃣ What is a ternary operator used for?
➡️ Used as a short form of if-else.
There are 8 main types of operators in Java, and here’s a clear, structured explanation of each — with syntax, examples, and outputs 👇
💡 Types of Operators in Java
| No. | Operator Type | Description |
|---|---|---|
| 1️⃣ | Unary Operator | Works on a single operand (like ++, --, +, -) |
| 2️⃣ | Arithmetic Operator | Performs mathematical operations (+, -, *, /, %) |
| 3️⃣ | Relational Operator | Compares two values (==, !=, >, <, >=, <=) |
| 4️⃣ | Ternary Operator | Shortcut for if-else condition (? :) |
| 5️⃣ | Assignment Operator | Assigns values (=, +=, -=, *=, /=, %=) |
| 6️⃣ | Bitwise Operator | Performs bit-level operations (&, |
| 7️⃣ | Logical Operator | Works on boolean values (&&, ||, !) |
| 8️⃣ | Shift Operator | Shifts bits left or right (<<, >>, >>>) |
1️⃣ Unary Operators
Used with one operand.
| Operator | Meaning | Example | Output |
|---|---|---|---|
| ++ | Increment | ++a | Increases value by 1 |
| -- | Decrement | --a | Decreases value by 1 |
| + | Unary plus | +a | Indicates positive |
| - | Unary minus | -a | Negates the value |
Example:
2️⃣ Arithmetic Operators
Used for mathematical calculations.
| Operator | Example | Result |
|---|---|---|
| + | 10 + 5 | 15 |
| - | 10 - 5 | 5 |
| * | 10 * 5 | 50 |
| / | 10 / 3 | 3 |
| % | 10 % 3 | 1 |
Example:
3️⃣ Relational Operators
Used to compare two values.
| Operator | Meaning | Example | Output |
|---|---|---|---|
| == | Equal to | a == b | true / false |
| != | Not equal to | a != b | true / false |
| > | Greater than | a > b | true / false |
| < | Less than | a < b | true / false |
| >= | Greater or equal | a >= b | true / false |
| <= | Less or equal | a <= b | true / false |
Example:
4️⃣ Ternary Operator
Used for conditional checking in a single line.
Syntax:
Example:
5️⃣ Assignment Operators
Used to assign values to variables.
| Operator | Example | Equivalent To |
|---|---|---|
| = | a = 5 | a = 5 |
| += | a += 5 | a = a + 5 |
| -= | a -= 5 | a = a - 5 |
| *= | a *= 5 | a = a * 5 |
| /= | a /= 5 | a = a / 5 |
| %= | a %= 5 | a = a % 5 |
Example:
6️⃣ Bitwise Operators
Operate on bits (0s and 1s).
| Operator | Meaning | Example | Result |
|---|---|---|---|
| & | AND | 5 & 3 | 1 |
| | | OR | 5 | 3 | 7 |
| ^ | XOR | 5 ^ 3 | 6 |
| ~ | NOT | ~5 | -6 |
Example:
7️⃣ Logical Operators
Used with boolean (true/false) values.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| && | AND | (a>5 && b>10) | true if both true |
| || | OR | (a>5 || b>10) | true if one is true |
| ! | NOT | !(a>5) | reverses result |
Example:
8️⃣ Shift Operators
Used to shift bits left or right.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| << | Left Shift | 10 << 1 | 20 |
| >> | Right Shift | 10 >> 1 | 5 |
| >>> | Unsigned Right Shift | -10 >>> 1 | large positive number |
Example:
⚙️ Operator Precedence (High → Low)
| Priority | Operator |
|---|---|
| 1 | ++, -- |
| 2 | *, /, % |
| 3 | +, - |
| 4 | <<, >>, >>> |
| 5 | <, <=, >, >= |
| 6 | ==, != |
| 7 | & |
| 8 | ^ |
| 9 | | |
| 10 | && |
| 11 | || |
| 12 | ?: |
| 13 | =, +=, -=, *=, /=, %= |