# Operators in Java

operators are present every programming language, operators are single character which does some specific task .for example + operator is used to add two numbers

## Types of Operators in Java

- Arithmetic Operators
- Logical Operators
- Auto-increment and Auto-decrement Operators
- Comparison (relational) operators
- Bitwise Operators
- Assignment Operators
- Ternary Operator

### Arithmetic Operators

Arithmetic operators are used to performing arithmetic operations like addition subtraction.

NO | symbol | name |
---|---|---|

1 | + | addition |

2 | – | subtraction |

3 | * | multiplication |

4 | / | division |

5 | % | modulo |

Modulo operator returns remainder, for example, 10 % 5 would return 0

#### Example for Arithmetic Operators

**OUTPUT**

### Logical Operators

Using logical operators we can check multiple conditions .this condition has two answers true or false ,for example “3 is odd or even number” the answer is true .there are so many multiple conditions in logical operators

operator | meaning | Example |
---|---|---|

&& | Logical AND | If c=1 and d=2 then, (c==1) && (d>1) equals to 0 |

! | Logical NOT | If c =1 then, expression ! (c == 1) equals to 0. |

|| | Logical OR | If c=1 and d=2 then, (c==1) || (d>1) equals to 1. |

if there are two conditions .the logical operators works as shown in below table

LOGICAL OPERATOR | FIRST CONDITION | SECOND CONDITION | RESULT |
---|---|---|---|

&& | true | true | true |

&& | true | false | false |

&& | false | true | false |

&& | false | false | false |

|| | true | true | true |

|| | true | false | true |

|| | false | true | true |

|| | false | false | false |

#### Logical Operators Example

**OUTPUT**

### Auto-increment and Auto-decrement Operators

these operators are used to increment a value by 1 (++) and it uis used to decrement a value by 1 by (–)

increment operator is implemented in two forms

#### 1)pre-increment

here, the value incremented first and and then assigned

example: ++a

#### 2)post-increment

here, the value is assigned first and the incremented

#### 3)pre-decrement

here, the value decremented first and and then assigned

#### 4 )post-decrement

here, the value is assigned first and then decremented

**Increment Decrement Example:**

**OUTPUT**

### Relational operators

Relational operators are used to comparing between two values .it evaluates to 1 if the condition is true and 0 for if it is false.

NO | symbol | meaning |
---|---|---|

1 | == | returns true if both the left side and right side are equal |

**2** | ****!=**** | returns true if the left side is not equal to the right side of the operator. |

3 | > | returns true if the left side is greater than right. |

4 | < | returns true if the left side is less than the right side. |

5 | >= | returns true if the left side is greater than or equal to the right side. |

6 | <= | returns true if the left side is less than or equal to the right side. |

#### Relational operators Example

**OUTPUT**

### Bitwise Operators

these are used to perform calculations on binary numbers. addition, subtraction, addition, and division are done in bit-level which makes processing faster and saves power

NO | operator | meaning |
---|---|---|

1 | & | Bitwise AND |

2 | ^ | Bitwise exclusive OR |

3 | | | Bitwise OR |

4 | ~ | Bitwise complement |

5 | >> and << | Shift right and left |

#### Bitwise AND operator &

the output of bitwise AND will be 1,if the both operands are 1 ,if either any one is 0 , the result of corresponding to that bit is evaluated to 0.

#### Bitwise OR operator |

this will return 1 ,when any one of the input is 1,otherwise returns zero.

#### Bitwise XOR (exclusive OR) operator ^

this returns 1,when the two operands are diffrent ,but it both are same it returns 0

#### Bitwise complement operator ~

this is an uniary operator,works on one operand . It changes the 1 to 0 and 0 to 1. It is denoted by ~.

#### Right Shift Operator >>

this is used to shift the bits in a given binary number towards right in a required no of positions ,denoted by >>

#### left shit operator <<

Left shift operator shifts all bits towards left by certain number of specified bits. It is denoted by <<.

### Assignment Operators

Operator | Step 1 example | Step 2 example |
---|---|---|

%= | a %= b | a = a%b |

*= | a *= b | a = a*b |

+= | a += b | a = a+b |

-= | a -= b | a = a-b |

/= | a /= b | a = a/b |

= | a = b | a = b |

#### Assignment Operators Example

**Output**

### Ternary Operator

This operator has three arguments ,the value will be returned based on boolean expression

**Syntax:**

if the boolean expression is true then First statement will be returned and if the boolean expression is false then Second statement will be returned

Ternary operator can be replaced with if-else. The below example can be written using if-else statement as shown below.

#### Ternary Operator Example

This can be replaced by the below code

**Output**