Operators in Java
Operators are symbols used to perform operations on variables and values. They help manipulate the data stored in variables and are essential to implement program logic.
Example:
int sum = 10 + 20; // '+' is an addition operator
or
int num1 = 10;
int num2 = 20;
int sum = num1 + num2;
// num1 and num2 → operands
// '+' → operator
Classification by Number of Operands
| Operator Type | Description | Example |
|---|---|---|
| Unary | Works with one operand | a++, --a, ++a, --a, !a |
| Binary | Works with two operands | a + b, a * b, a / b |
| Ternary | Works with three operands (Conditional operator) | (a > b) ? a : b |
Note:
Binary operators include many categories like arithmetic (+, -, *, /), relational (==, <, >), logical (&&, ||), and assignment (=, +=, -=).
They all work on two operands, which is why they are grouped under binary operators.
Classification by Functionality
1. Arithmetic Operators
Used to perform basic mathematical operations.
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus | a % b |
2. Relational (Comparison) Operators
Used to compare two values. Result is always true or false.
| Operator | Meaning | 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
Used to perform logical operations, mostly with boolean values.
| Operator | Meaning | Example |
|---|---|---|
| && | Logical AND | a > 5 && b < 10 |
| || | Logical OR | a==0 || a==10 |
| ! | Logical NOT | !(a > b) |
4. Assignment Operators
Used to assign values to variables.
| Operator | Meaning | Example |
|---|---|---|
| = | Assign | a = 10 |
| += | Add and assign | a += 5 → a = a + 5 |
| -= | Subtract and assign | a -= 3 |
| *= | Multiply and assign | a *= 2 |
| /= | Divide and assign | a /= 4 |
| %= | Modulus and assign | a %= 2 |
5. Unary Operators
Operators that act on a single operand.
| Operator | Meaning | Example |
|---|---|---|
| + | Unary plus (positive sign) | +a |
| - | Unary minus (negation) | -a |
| ++ | Increment (pre/post) | ++a, a++ |
| -- | Decrement (pre/post) | --a, a-- |
| ! | Logical NOT | !true |
6. Bitwise Operators
Operate at the bit level (mainly for integer types).
| Operator | Meaning | Example |
|---|---|---|
| & | Bitwise AND | a & b |
| | | Bitwise OR | a | b |
| ^ | Bitwise XOR | a ^ b |
| ~ | Bitwise Complement | ~a |
| << | Left shift | a << 2 |
| >> | Right shift | a >> 2 |
| >>> | Unsigned right shift | a >>> 2 |
7. Ternary Operator
A shortcut for if-else conditions.
| Operator | Meaning | Example |
|---|---|---|
| ? : | Ternary (conditional) | int max = (a > b) ? a : b; |
8. instanceof Operator
Used to check if an object belongs to a specific class or subclass.
| Operator | Example |
|---|---|
| instanceof | str instanceof String |
Summary:
- Operators are essential for performing operations and making decisions in Java programs.
- Java supports unary, binary, and ternary operators.
- Operators are classified by functionality like arithmetic, relational, logical, bitwise, etc.
- Knowing how each operator works helps in writing clean, efficient, and correct code.