Operators in Java are special symbols or keywords that are used to perform operations on variables and values. Java supports a wide range of operators, categorized into several types:
### 1. Arithmetic Operators:
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division, and modulus (remainder).
- **Addition (`+`)**: Adds two operands.
- **Subtraction (`-`)**: Subtracts the right operand from the left operand.
- **Multiplication (`*`)**: Multiplies two operands.
- **Division (`/`)**: Divides the left operand by the right operand.
- **Modulus (`%`)**: Returns the remainder of the division operation.
int a = 10, b = 3;
int sum = a + b; // 13
int difference = a - b; // 7
int product = a * b; // 30
int quotient = a / b; // 3
int remainder = a % b; // 1
```
### 2. Relational Operators:
Relational operators compare two values and return a boolean result (`true` or `false`).
- **Equal to (`==`)**: Checks if two operands are equal.
- **Not equal to (`!=`)**: Checks if two operands are not equal.
- **Greater than (`>`)**: Checks if the left operand is greater than the right operand.
- **Less than (`<`)**: Checks if the left operand is less than the right operand.
- **Greater than or equal to (`>=`)**: Checks if the left operand is greater than or equal to the right operand.
- **Less than or equal to (`<=`)**: Checks if the left operand is less than or equal to the right operand.
int x = 5, y = 10;
boolean isEqual = (x == y); // false
boolean isNotEqual = (x != y); // true
boolean isGreaterThan = (x > y); // false
boolean isLessThan = (x < y); // true
boolean isGreaterOrEqual = (x >= y); // false
boolean isLessOrEqual = (x <= y); // true
```
### 3. Logical Operators:
Logical operators are used to combine multiple conditions or to reverse the logical state of an expression.
- **Logical AND (`&&`)**: Returns `true` if both operands are `true`.
- **Logical OR (`||`)**: Returns `true` if at least one operand is `true`.
- **Logical NOT (`!`)**: Reverses the logical state of the operand.
boolean condition1 = true, condition2 = false;
boolean result1 = condition1 && condition2; // false
boolean result2 = condition1 || condition2; // true
boolean result3 = !condition1; // false (negates condition1)
```
### 4. Assignment Operators:
Assignment operators are used to assign values to variables.
- **Assignment (`=`)**: Assigns the value on the right to the variable on the left.
- **Compound Assignment (`+=`, `-=`, `*=`, `/=`, `%=`)**: Combines an arithmetic operation with assignment.
int num = 10;
num += 5; // num = num + 5; (num is now 15)
num -= 3; // num = num - 3; (num is now 12)
num *= 2; // num = num * 2; (num is now 24)
num /= 4; // num = num / 4; (num is now 6)
num %= 5; // num = num % 5; (num is now 1)
```
### 5. Unary Operators:
Unary operators operate on a single operand.
- **Unary Plus (`+`)**: Indicates positive value (default).
- **Unary Minus (`-`)**: Negates the value.
- **Increment (`++`)**: Increases the value by 1.
- **Decrement (`--`)**: Decreases the value by 1.
int value = 10;
int positiveValue = +value; // 10 (unary plus)
int negativeValue = -value; // -10 (unary minus)
value++; // value is now 11 (post-increment)
value--; // value is now 10 (post-decrement)
```
### 6. Bitwise Operators (Advanced):
Bitwise operators perform operations at the bit level on integers.
- **Bitwise AND (`&`)**: Performs a bitwise AND.
- **Bitwise OR (`|`)**: Performs a bitwise OR.
- **Bitwise XOR (`^`)**: Performs a bitwise XOR (exclusive OR).
- **Bitwise NOT (`~`)**: Inverts bits.
- **Left Shift (`<<`)**: Shifts bits to the left.
- **Right Shift (`>>`)**: Shifts bits to the right.
- **Unsigned Right Shift (`>>>`)**: Shifts bits to the right, filling leftmost bits with zeros.
These operators are used in low-level programming and scenarios involving manipulation of bits.
Understanding these operators is essential for writing effective Java programs, as they enable you to perform a wide range of operations from simple arithmetic to complex bitwise manipulations.