# Operators

- Operators are special symbols in Java that perform specific operations on one, two, or three operands, and then return a result.
- Operands are the values or variables involved in the operation.

## Types of Operators in Java

Java provides a rich set of operators to manipulate variables. These operators can be categorized into the following groups:

*Arithmetic Operators**Relational Operators**Logical Operators**Assignment Operators**Unary Operators**Bitwise Operators**Ternary Operator*

### 1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

Operator | Description | Example |
---|---|---|

`+` | Addition | `a + b` |

`-` | Subtraction | `a - b` |

`*` | Multiplication | `a * b` |

`/` | Division | `a / b` |

`%` | Modulus (remainder) | `a % b` |

**Example:**

`int a = 10;`

int b = 5;

int sum = a + b; // sum is 15

int difference = a - b; // difference is 5

int product = a * b; // product is 50

int quotient = a / b; // quotient is 2

int remainder = a % b; // remainder is 0

### 2. Relational Operators

Relational operators compare two values and return a boolean result (`true`

or `false`

).

Operator | Description | Example |
---|---|---|

`==` | Equal to | `a == b` |

`!=` | Not equal to | `a != b` |

`>` | Greater than | `a > b` |

`<` | Less than | `a < b` |

`>=` | Greater than or equal to | `a >= b` |

`<=` | Less than or equal to | `a <= b` |

**Example:**

`int a = 10;`

int b = 5;

boolean result1 = a == b; // result1 is false

boolean result2 = a != b; // result2 is true

boolean result3 = a > b; // result3 is true

boolean result4 = a < b; // result4 is false

boolean result5 = a >= b; // result5 is true

boolean result6 = a <= b; // result6 is false

### 3. Logical Operators

Logical operators are used to combine multiple boolean expressions.

Operator | Description | Example |
---|---|---|

`&&` | Logical AND | `a && b` |

` | ` | |

`!` | Logical NOT | `!a` |

**Example:**

`boolean x = true;`

boolean y = false;

boolean result1 = x && y; // result1 is false

boolean result2 = x || y; // result2 is true

boolean result3 = !x; // result3 is false

### 4. Assignment Operators

Assignment operators are used to assign values to variables.

Operator | Description | Example |
---|---|---|

`=` | Assignment | `a = 10` |

`+=` | Add and assign | `a += 5` |

`-=` | Subtract and assign | `a -= 5` |

`*=` | Multiply and assign | `a *= 5` |

`/=` | Divide and assign | `a /= 5` |

`%=` | Modulus and assign | `a %= 5` |

**Example:**

`int a = 10;`

a += 5; // a is now 15

a -= 3; // a is now 12

a *= 2; // a is now 24

a /= 4; // a is now 6

a %= 3; // a is now 0

### 5. Unary Operators

Unary operators operate on a single operand to perform various operations.

Operator | Description | Example |
---|---|---|

`+` | Unary plus | `+a` |

`-` | Unary minus | `-a` |

`++` | Increment | `a++` or `++a` |

`--` | Decrement | `a--` or `--a` |

`!` | Logical NOT | `!a` |

**Example:**

`int a = 10;`

int b = ++a; // a is 11, b is 11 (pre-increment)

int c = a--; // a is 10, c is 11 (post-decrement)

boolean d = !true; // d is false

### 6. Bitwise Operators

Bitwise operators perform operations on the binary representations of integers.

Operator | Description | Example |
---|---|---|

`&` | Bitwise AND | `a & b` |

` | ` | Bitwise OR |

`^` | Bitwise XOR | `a ^ b` |

`~` | Bitwise complement | `~a` |

`<<` | Left shift | `a << 2` |

`>>` | Right shift | `a >> 2` |

`>>>` | Unsigned right shift | `a >>> 2` |

**Example:**

`int a = 5; // 0101 in binary`

int b = 3; // 0011 in binary

int c = a & b; // c is 1 (0001 in binary)

int d = a | b; // d is 7 (0111 in binary)

int e = a ^ b; // e is 6 (0110 in binary)

int f = ~a; // f is -6 (inverts all bits)

int g = a << 1; // g is 10 (1010 in binary)

int h = a >> 1; // h is 2 (0010 in binary)

int i = a >>> 1; // i is 2 (0010 in binary)

### 7. Ternary Operator

The ternary operator is a shorthand for the `if-else`

statement. It takes three operands.

Operator | Description | Example |
---|---|---|

`? :` | Ternary (conditional) operator | `condition ? value1 : value2` |

**Example:**

`int a = 10;`

int b = 5;

int max = (a > b) ? a : b; // max is 10

## Summary

Operators in Java are symbols that perform operations on variables and values. Here’s a quick recap of what we've covered:

**Arithmetic Operators:**+, -, *, /, %**Relational Operators:**==, !=, >, <, >=, <=**Logical Operators:**&&, ||, !**Assignment Operators:**=, +=, -=, *=, /=, %=**Unary Operators:**+, -, ++, --, !**Bitwise Operators:**&, |, ^, ~, <<, >>, >>>**Ternary Operator:**? :