Assignment Operator
⇒ Assigns the right-hand operand's value to the left-hand operand.
Example: int a = 5;
Arithmetic Operators
| Operator | Name | Description | Example | Result |
|---|---|---|---|---|
+ |
Addition | Adds two values | 5 + 3 |
8 |
- |
Subtraction | Subtracts second value from first | 5 - 3 |
2 |
* |
Multiplication | Multiplies two values | 5 * 3 |
15 |
/ |
Division | Divides first value by second | 6 / 3 |
2 |
% |
Modulus (Rem) | Returns the remainder of division | 7 % 3 |
1 |
++ |
Increment | Increases value by 1 | a++ or ++a |
a = a + 1 |
-- |
Decrement | Decreases value by 1 | a-- or --a |
a = a - 1 |
Order of Operation
| Precedence Level | Operators | Type | Associativity |
|---|---|---|---|
| 1 (Highest) | () |
Parentheses (Grouping) | Left to Right |
| 2 | ++, --, +, -, ~, ! |
Unary | Right to Left |
| 3 | *, /, % |
Multiplicative | Left to Right |
| 4 | +, - |
Additive | Left to Right |
| 5 | <<, >>, >>> |
Shift | Left to Right |
| 6 | <, <=, >, >=, instanceof |
Relational | Left to Right |
| 7 | ==, != |
Equality | Left to Right |
| 8 | & |
Bitwise AND | Left to Right |
| 10 | ` | ` | Bitwise OR |
| 9 | ^ |
Bitwise XOR | Left to Right |
| 11 | && |
Logical AND | Left to Right |
| 12 | ` | ` | |
| 13 | ? : |
Ternary (Conditional) | Right to Left |
| 14 | =, +=, -=, *=, /=, %= etc. |
Assignment | Right to Left |
| 15 (Lowest) | , |
Comma | Left to Right |
Shorthand Operators
| Operator | Meaning | Equivalent To | Example | Result |
|---|---|---|---|---|
= |
Assignment | a = b |
a = 5 |
a becomes 5 |
+= |
Add and assign | a = a + b |
a += 3 |
a increases by 3 |
-= |
Subtract and assign | a = a - b |
a -= 2 |
a decreases by 2 |
*= |
Multiply and assign | a = a * b |
a *= 4 |
a is multiplied by 4 |
/= |
Divide and assign | a = a / b |
a /= 2 |
a is divided by 2 |
%= |
Modulo and assign | a = a % b |
a %= 3 |
a becomes remainder |
&= |
Bitwise AND and assign | a = a & b |
a &= b |
Bitwise AND result |
| ` | =` | Bitwise OR and assign | `a = a | b` |
^= |
Bitwise XOR and assign | a = a ^ b |
a ^= b |
Bitwise XOR result |
<<= |
Left shift and assign | a = a << b |
a <<= 2 |
Shift left by 2 bits |
>>= |
Right shift and assign | a = a >> b |
a >>= 1 |
Shift right by 1 bit |
>>>= |
Unsigned right shift | a = a >>> b |
a >>>= 1 |
Logical shift right |
Unary Operators
| Operator | Name | Description | Example | Result |
|---|---|---|---|---|
+ |
Unary Plus | Indicates a positive value (optional, rarely used) | +a |
Same as a |
- |
Unary Minus | Negates the value of the operand | -a |
Negative of a |
++ |
Increment | Increases value by 1 | a++ / ++a |
a becomes a + 1 |
-- |
Decrement | Decreases value by 1 | a-- / --a |
a becomes a - 1 |
! |
Logical NOT | Inverts a boolean value | !true |
false |
~ |
Bitwise Complement | Inverts each bit of the integer | ~5 |
Result: -6 |
If-else
⇒ Syntax: Uses if () {} to check a condition.
⇒ What is if: Executes block if condition is true, skips if false.
⇒ What is else: Executes a block when the if condition is false.
⇒ Curly Braces can be omitted for single statements, but not recommended.
⇒ If-else Ladder: Multiple if and else if blocks; only one executes.
⇒ Use Variables: Can store conditions in variables for use in if statements
Relational Operators
⇒ Equality
→ == Checks value equality.
⇒ Inequality
→ != Checks value inequality.
⇒ Relational
→ > Greater than. → < Less than. → >= Greater than or equal to. → <= Less than or equal to.
<aside> 🔒
Order of Relational operators is less than arithmetic operators
</aside>
Logical Operators
⇒ AND (&&):
→ All conditions must be true for the result to be true.
⇒ OR (||):
→ Only one condition must be true for the result to be true.
⇒ NOT (!):
→ Inverts the Boolean value of a condition.
<aside> 🔒
Lower Priority than Math and Comparison operators
</aside>
Operator Precedence
⇒ Operator Precedence
→ Determines the evaluation order of operators in an expression based on their priority levels.
⇒ Associativity
→ Defines the order of operation for operators with the same precedence, usually left-to-right or right-to-left.