Operators in Java are symbols used to perform operations on variables and values. Java supports a rich set of operators used in expressions to manipulate data and control program logic.
🧮 What is an Operator?
An operator performs a specific action on one or more operands.
🗂️ Categories of Java Operators
| Category | Description |
|---|
| Arithmetic | Basic math operations |
| Relational (Comparison) | Compare two values |
| Logical | Combine boolean expressions |
| Assignment | Assign values |
| Unary | Operate on a single operand |
| Bitwise | Operate on bits |
| Ternary | Short form of if-else |
| Shift | Shift bits left/right |
1️⃣ Arithmetic Operators
| Operator | Description | Example |
|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
2️⃣ Relational (Comparison) Operators
| Operator | Description | Example |
|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
3️⃣ Logical Operators
| Operator | Description | Example |
|---|
&& | Logical AND | a > 0 && b > 0 |
| ` | | ` |
! | Logical NOT | !(a > 0) |
4️⃣ Assignment Operators
| Operator | Example | Equivalent To |
|---|
= | a = 10 | assign 10 to a |
+= | a += 5 | a = a + 5 |
-= | a -= 3 | a = a - 3 |
*= | a *= 2 | a = a * 2 |
/= | a /= 4 | a = a / 4 |
%= | a %= 2 | a = a % 2 |
5️⃣ Unary Operators
| Operator | Description | Example |
|---|
+ | Positive | +a |
- | Negative | -a |
++ | Increment | a++ or ++a |
-- | Decrement | a-- or --a |
! | Boolean NOT | !true = false |
6️⃣ Ternary Operator
Shorthand for if-else.
7️⃣ Bitwise Operators (Advanced)
| Operator | Description | Example |
|---|
& | AND | a & b |
| ` | ` | OR |
^ | XOR | a ^ b |
~ | NOT | ~a |
<< | Left shift | a << 2 |
>> | Right shift | a >> 2 |
Used mostly in low-level or performance-sensitive code.
🧪 Code Example
🧾 Summary Table
| Type | Operators |
|---|
| Arithmetic | +, -, *, /, % |
| Relational | ==, !=, >, <, >=, <= |
| Logical | &&, ` |
| Assignment | =, +=, -=, etc. |
| Unary | ++, --, -, +, ! |
| Ternary | condition ? true : false |
| Bitwise | &, ` |