✅ Java Operators – Complete Guide
In Java, operators are special symbols used to perform operations on variables and values. They are grouped into several categories:
🔹 1. Arithmetic Operators
Used for basic mathematical operations.
Operator | Description | Example (a = 10 , b = 5 ) | Result |
---|
+ | Addition | a + b | 15 |
- | Subtraction | a - b | 5 |
* | Multiplication | a * b | 50 |
/ | Division | a / b | 2 |
% | Modulus (Remainder) | a % b | 0 |
🔹 2. Relational (Comparison) Operators
Used to compare two values, returning a boolean (true
or false
).
Operator | Description | Example | Result |
---|
== | Equal to | a == b | false |
!= | Not equal to | a != b | true |
> | Greater than | a > b | true |
< | Less than | a < b | false |
>= | Greater than or equal | a >= b | true |
<= | Less than or equal | a <= b | false |
🔹 3. Logical Operators (used with boolean values)
Operator | Name | Description | Example |
---|
&& | Logical AND | True if both conditions are true | (a > 0 && b > 0) → true |
|| |
| True if any one of the conditions is true | (a > 0 || b > 0) → true |
! | Logical NOT | Reverses the boolean result | !(a > b) → true if a < b |
📝
Note:
&&
and
||
use
short-circuit evaluation:
-
A || B
: If A
is true
, B
is not evaluated.
-
A && B
: If A
is false
, B
is not evaluated.
🔹 4. Bitwise Operators (used with integer types for binary operations)
Operator | Name | Description | Example (a = 5 , b = 3 ) | Result |
---|
& | AND | 1 if both bits are 1 | a & b = 0101 & 0011 = 0001 → 1 | |
| | OR |
| 1 if at least one bit is 1 | `a |
^ | XOR | 1 if bits are different | a ^ b = 0101 ^ 0011 = 0110 → 6 | |
~ | NOT | Inverts bits | ~a = ~0101 = 1010 | |
<< | Left shift | Shifts bits to the left (×2 per shift) | a << 1 = 1010 = 10 | |
>> | Right shift | Shifts bits to the right (÷2 per shift) | a >> 1 = 0010 = 2 | |
>>> | Unsigned right shift | Same as >> but fills left with 0 | a >>> 1 | |
🔹 5. Assignment Operators
Used to assign values or update a variable using an operation.
Operator | Example | Meaning |
---|
= | a = b | Assign b to a |
+= | a += b | a = a + b |
-= | a -= b | a = a - b |
*= | a *= b | a = a * b |
/= | a /= b | a = a / b |
%= | a %= b | a = a % b |
&= | a &= b | a = a & b |
` | =` | `a |
^= | a ^= b | a = a ^ b |
<<= | a <<= b | a = a << b |
>>= | a >>= b | a = a >> b |
>>>= | a >>>= b | a = a >>> b |
🔹 6. Unary Operators
Operate on a single operand.
Operator | Description | Example |
---|
+ | Unary plus | +a |
- | Unary minus | -a |
++ | Increment (prefix or postfix) | ++a , a++ |
-- | Decrement (prefix or postfix) | --a , a-- |
! | Logical NOT | !true → false |
~ | Bitwise NOT | ~a |
🔹 7. Ternary Operator
A short-hand for if-else
.
This means:
If a > b
→ max = a
, else → max = b
.
🔹 8. Type Comparison Operator
Operator | Description | Example |
---|
instanceof | Checks object type | s instanceof String → true |
🔹 9. Other Special Operators
Operator | Description |
---|
. | Member access (dot) |
[] | Array access |
() | Method call |
new | Object creation |
:: | Method reference (Java 8+) |
Sample Code to Demonstrate Key Operators
Comments
Post a Comment