## Java Operators

Operator is a symbol which tells to the compiler to perform some operation. Java provides a rich set of operators do deal with various types of operations. Sometimes we need to perform arithmetic operations then we use plus (+) operator for addition, multiply(*) for multiplication etc.

Operators are always essential part of any programming language.

Java operators can be divided into following categories:

- Arithmetic operators
- Relation operators
- Logical operators
- Bitwise operators
- Assignment operators
- Conditional operators
- Misc operators

## Arithmetic operators

Arithmetic operators are used to perform arithmetic operations like: addition, subtraction etc and helpful to solve mathematical expressions. The below table contains Arithmetic operators.

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

`+` |
adds two operands |

`-` |
subtract second operands from first |

`*` |
multiply two operand |

`/` |
divide numerator by denumerator |

`%` |
remainder of division |

`++` |
Increment operator increases integer value by one |

`--` |
Decrement operator decreases integer value by one |

**Example:**

Lets create an example to understand arithmetic operators and their operations.

## Relation operators

Relational operators are used to test comparison between operands or values. It can be use to test whether two values are equal or not equal or less than or greater than etc.

The following table shows all relation operators supported by Java.

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

`==` |
Check if two operand are equal |

`!=` |
Check if two operand are not equal. |

`>` |
Check if operand on the left is greater than operand on the right |

`<` |
Check operand on the left is smaller than right operand |

`>=` |
check left operand is greater than or equal to right operand |

`<=` |
Check if operand on left is smaller than or equal to right operand |

**Example:**

In this example, we are using relational operators to test comparison like less than, greater than etc.

## Logical operators

Logical Operators are used to check conditional expression. For example, we can use logical operators in if statement to evaluate conditional based expression. We can use them into loop as well to evaluate a condition.

Java supports following 3 logical operator. Suppose we have two variables whose values are: **a=true** and **b=false**.

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

`&&` |
Logical AND | (a && b) is false |

`||` |
Logical OR | (a || b) is true |

`!` |
Logical NOT | (!a) is false |

**Example:**

In this example, we are using logical operators. There operators return either true or false value.

## Bitwise operators

Bitwise operators are used to perform operations bit by bit.

Java defines several bitwise operators that can be applied to the integer types long, int, short, char and byte.

The following table shows all bitwise operators supported by Java.

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

`&` |
Bitwise AND |

`|` |
Bitwise OR |

`^` |
Bitwise exclusive OR |

`<<` |
left shift |

`>>` |
right shift |

Now lets see truth table for bitwise `&`

, `|`

and `^`

`a` |
`b` |
`a & b` |
`a | b` |
`a ^ b` |
---|---|---|---|---|

0 | 0 | 0 | 0 | 0 |

0 | 1 | 0 | 1 | 1 |

1 | 0 | 0 | 1 | 1 |

1 | 1 | 1 | 1 | 0 |

The bitwise **shift operators shifts the bit value**. The **left operand specifies the value to be shifted** and the **right operand specifies the number of positions** that the bits in the value are to be shifted. Both operands have the same precedence.

**Example:**

Lets create an example that shows working of bitwise operators.

## Assignment Operators

Assignment operators are used to assign a value to a variable. It can also be used combine with arithmetic operators to perform arithmetic operations and then assign the result to the variable. Assignment operator supported by Java are as follows:

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

`=` |
assigns values from right side operands to left side operand | `a = b` |

`+=` |
adds right operand to the left operand and assign the result to left | `a+=b` is same as `a=a+b` |

`-=` |
subtracts right operand from the left operand and assign the result to left operand | `a-=b` is same as a`=a-b` |

`*=` |
mutiply left operand with the right operand and assign the result to left operand | `a*=b` is same as `a=a*b` |

`/=` |
divides left operand with the right operand and assign the result to left operand | `a/=b` is same as `a=a/b` |

`%=` |
calculate modulus using two operands and assign the result to left operand | `a%=b` is same as `a=a%b` |

**Example:**

Lets create an example to understand use of assignment operators. All assignment operators have right to left associativity.

## Misc. operator

There are few other operator supported by java language.

### Conditional operator

It is also known as ternary operator because it works with **three operands. It is short alternate of if-else statement.** It can be used to evaluate Boolean expression and **return either true or false** value

**Example:**

In ternary operator, if **epr1** is true then expression evaluates after **question mark (?)** else evaluates **after colon (:)**. See the below example.

`instanceOf`

operator

It is a java **keyword** and used to test whether the given **reference belongs to provided type** or not. Type can be a class or interface. **It returns either true or false**.

Example:

Here, we created a string reference variable that stores “onlineexamguide”. Since it stores string value so we test it using is instance operator to check whether it belongs to string class or not. See the below example.