Java Operators – A Complete Guide
Operators in Java are special symbols that perform operations on variables and values. They help in performing arithmetic, logical, bitwise, and other computations in Java programs. In this post, we will explore different types of operators with examples.
1. Types of Java Operators
Java operators can be categorized into the following types:
- Arithmetic Operators
- Relational (Comparison) Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Unary Operators
- Ternary Operator
- Instanceof Operator
Let’s dive into each type with examples.
2. Arithmetic Operators
Used for mathematical operations like addition, subtraction, multiplication, etc.
| 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 |
Example:
3. Relational (Comparison) Operators
Used to compare values and return a boolean result (true or false).
| Operator | Description | Example (a = 10, b = 5) | 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 to | a >= b | true |
<= | Less than or equal to | a <= b | false |
Example:
4. Logical Operators
Used to combine multiple conditions and return a boolean result.
| Operator | Description | Example (x = true, y = false) | Result |
|---|---|---|---|
&& | AND | x && y | false |
| ` | ` | OR | |
! | NOT | !x | false |
Example:
5. Bitwise Operators
Operate at the bit level to perform binary calculations.
| Operator | Description | Example (a = 5, b = 3) | Result |
|---|---|---|---|
& | AND | a & b | 1 |
| ` | ` | OR | `a |
^ | XOR | a ^ b | 6 |
~ | Complement | ~a | -6 |
<< | Left shift | a << 1 | 10 |
>> | Right shift | a >> 1 | 2 |
Example:
6. Assignment Operators
Used to assign values to variables.
| Operator | Description | Example (a = 10) | Equivalent To |
|---|---|---|---|
= | Assign | a = 10 | — |
+= | Add and assign | a += 5 | a = a + 5 |
-= | Subtract and assign | a -= 5 | a = a - 5 |
*= | Multiply and assign | a *= 5 | a = a * 5 |
/= | Divide and assign | a /= 5 | a = a / 5 |
%= | Modulus and assign | a %= 5 | a = a % 5 |
Example:
7. Unary Operators
Used with a single operand.
| Operator | Description | Example (a = 10) | Result |
|---|---|---|---|
+ | Unary plus | +a | 10 |
- | Unary minus | -a | -10 |
++ | Increment | ++a | 11 |
-- | Decrement | --a | 9 |
Example:
8. Ternary Operator (? :)
A shorthand for if-else.
Syntax:
Example:
9. instanceof Operator
Used to check whether an object belongs to a specific class.
Example:
Conclusion
Operators are essential for performing computations in Java. Mastering them will help you write efficient and concise code. In the next post, we’ll explore Control Flow Statements in Java, including loops and conditional statements.
Comments
Post a Comment