Java Operators
Java Operators
Operators in Java are special symbols used to perform operations on variables and values.
Java provides several types of operators:
1. Arithmetic Operators
Used to perform mathematical operations.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | Division | 10 / 5 | 2 |
% | Modulus (Remainder) | 10 % 3 | 1 |
Example:
2. Assignment Operators
Used to assign values to variables.
| Operator | Meaning | Example |
|---|---|---|
= | Assign | x = 5 |
+= | Add and assign | x += 3 // x = x + 3 |
-= | Subtract and assign | x -= 3 |
*= | Multiply and assign | x *= 3 |
/= | Divide and assign | x /= 3 |
%= | Modulus and assign | x %= 3 |
3. Comparison (Relational) Operators
Used to compare two values.
| Operator | Meaning | Example |
|---|---|---|
== | Equal to | x == y |
!= | Not equal | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater or equal | x >= y |
<= | Less or equal | x <= y |
Example:
4. Logical Operators
Used to combine multiple conditions.
| Operator | Meaning | Example |
|---|---|---|
&& | Logical AND | (x > 5 && y < 10) |
| ` | ` | |
! | Logical NOT | !(x > 5) |
Example:
5. Unary Operators
Used with a single operand.
| Operator | Meaning |
|---|---|
+ | Positive |
- | Negative |
++ | Increment |
-- | Decrement |
! | Logical NOT |
Example:
6. Bitwise Operators
Used to perform operations on bits.
| Operator | Meaning |
|---|---|
& | Bitwise AND |
| ` | ` |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Left shift |
>> | Right shift |
7. Ternary (Conditional) Operator
Short form of if-else.
