Operators in Java are symbols used to perform operations on variables and values. Whether you're doing arithmetic, comparing values, or writing conditions, operators are fundamental to Java programming.
๐ What Is an Operator?
An operator is a symbol that tells the compiler to perform specific mathematical or logical operations.
Example:
int c = a + b; // '+' is an operator
๐ฆ What Is an Operand?
Operands are the values or variables on which operators work.
In the expression A + B = C, A, B, and C are operands.
๐งฑ Operator Classification
Java categorizes operators into three arity types:
- Unary โ requires one operand (
++a) - Binary โ requires two operands (
a + b) - Ternary โ requires three operands (
condition ? val1 : val2)
๐ข Types of Operators in Java
1. Arithmetic Operators
Used to perform basic mathematical operations.
Unary Operators
| Operator | Description | Example |
|---|---|---|
++ |
Increment | a++ is a = a + 1 |
-- |
Decrement | a-- is a = a - 1 |
! |
Logical NOT | !true becomes false |
Binary Operators
| Operator | Description |
|---|---|
+ |
Addition / String Concatenation |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulus (Remainder) |
2. Assignment Operators
Assign values to variables and combine assignment with other operations.
| Operator | Meaning | Example |
|---|---|---|
= |
Simple Assignment | a = 5 |
+= |
Addition + Assignment | a += 3 โ a = a + 3 |
-= |
Subtraction + Assignment | a -= 2 |
*= |
Multiplication + Assignment | a *= 2 |
/= |
Division + Assignment | a /= 4 |
%= |
Modulus + Assignment | a %= 2 |
<<=, >>=, &=, ^=, |= |
Bitwise assignment combos | a |= 2 |
3. Relational Operators
Used to compare two values.
| Operator | Meaning | Example |
|---|---|---|
== |
Equal to | a == b |
!= |
Not equal | a != b |
> |
Greater than | a > b |
< |
Less than | a < b |
>= |
Greater than or equal to | a >= b |
<= |
Less than or equal to | a <= b |
4. Logical Operators
Combine multiple conditions.
| Operator | Name | Example |
|---|---|---|
&& |
Logical AND | (a > 0 && b > 0) |
|| |
Logical OR | (a > 0 || b > 0) |
! |
Logical NOT | !(a > b) |
5. Bitwise Operators
Operate at the binary level.
| Operator | Description | Example |
|---|---|---|
& |
AND | a & b |
| |
OR | a | b |
^ |
XOR | a ^ b |
~ |
Bitwise NOT | ~a |
<< |
Left shift | a << 2 |
>> |
Right shift | a >> 2 |
>>> |
Zero-fill right shift | a >>> 2 |
6. instanceof Operator
Used to test if an object is an instance of a class or interface.
if (obj instanceof String) { ... }
7. Conditional (Ternary) Operator
Short form of an if-else condition:
int result = (a > b) ? a : b;
๐งฎ Operator Precedence and Associativity
Operator precedence determines the order of execution in expressions.
| Precedence | Operators | Associativity |
|---|---|---|
| 15 | () [] . |
Left to Right |
| 14 | ++ -- (postfix) |
Left to Right |
| 13 | ++ -- + - ! ~ |
Right to Left |
| 12 | * / % |
Left to Right |
| 11 | + - |
Left to Right |
| 10 | << >> >>> |
Left to Right |
| 9 | < <= > >= instanceof |
Left to Right |
| 8 | == != |
Left to Right |
| 7 | & |
Left to Right |
| 6 | ^ |
Left to Right |
| 5 | | |
Left to Right |
| 4 | && |
Left to Right |
| 3 | || |
Left to Right |
| 2 | ? : |
Right to Left |
| 1 | = += -= etc. |
Right to Left |
Tip: Use parentheses
()to clarify precedence in complex expressions.
๐ ๏ธ Real-World Example
double bill = 400;
double discount = (bill > 300) ? 0.10 : 0.05;
bill = bill - (bill * discount);
Use ternary instead of if-else for short decisions.
๐ง Interview Perspective
- What's the difference between
==and.equals()? - How does short-circuit evaluation work with
&&and||? - Explain
instanceofin polymorphism context.
๐งญ Java Version Relevance
| Feature | Version | Notes |
|---|---|---|
instanceof operator |
Java 1.0 | Originally added |
instanceof pattern matching |
Java 16 | if (x instanceof String s) โ no cast needed |
| Ternary operator | Java 1.0 | Always supported |
| Precedence rules | Java 1.0 | Core part of the language |
โ Summary
- Java provides a rich set of operators: arithmetic, assignment, relational, logical, bitwise, ternary, and more.
- Understanding precedence avoids bugs in complex expressions.
- Most operators follow mathematical logic but behave slightly differently in short-circuiting and bitwise contexts.
Mastering operators is the foundation for writing correct and optimized Java code.