# Java Operators

Operators perform operations on variables and values that is called Java Operators.

### Types of Operator

There are many types of operators in Java that is given below:

• Unary Operator,
• Arithmetic Operator,
• Shift Operator,
• Relational Operator,
• Bitwise Operator,
• Logical Operator,
• Ternary Operator and
• Assignment Operator.

## Operator Precedence in Java

Operator TypeCategoryPrecedence
Unarypostfix`expr++ expr--`
prefix`++expr --expr +expr -expr ~ !`
Arithmeticmultiplicative`* / %`
additive`+ -`
Shiftshift`<< >> >>>`
Relationalcomparison`< > <= >= instanceof`
equality`== !=`
Bitwisebitwise AND`&`
bitwise exclusive OR`^`
bitwise inclusive OR`|`
Logicallogical AND`&&`
logical OR`||`
Ternaryternary`? :`
Assignmentassignment`= += -= *= /= %= &= ^= |= <<= >>= >>>=`

## 1) Unary Operator in Java

The Java unary operators require only one operand. Unary operators are used to perform various operations i.e.:

• incrementing/decrementing a value by one
• negating an expression
• inverting the value of a boolean

Output:

```10
12
12
10
```

Output:

```22
21
```

Output:

```-11
9
false
true
```

### Java Arithmetic Operators

Java arithmatic operators are used to perform addition, subtraction, multiplication, and division. They act as basic mathematical operations.

Output:

```15
5
50
2
0
```

Output:

```21
```

### Java Left Shift Operator

The Java left shift operator << is used to shift all of the bits in a value to the left side of a specified number of times.

Output:

```40
80
80
240
```

### Java Right Shift Operator

The Java right shift operator >> is used to move left operands value to right by the number of bits specified by the right operand.

Output:

```2
5
2
```

Output:

```5
5
-5
1073741819
```

### Java AND Operator Example: Logical && and Bitwise &

The logical && operator doesn't check second condition if first condition is false. It checks second condition only if first one is true.

The bitwise & operator always checks both conditions whether first condition is true or false.

Output:

```false
false
```

Output:

```false
10
false
11
```

### Java OR Operator Example: Logical || and Bitwise |

The logical || operator doesn't check second condition if first condition is true. It checks second condition only if first one is false.

The bitwise | operator always checks both conditions whether first condition is true or false.

Output:

```true
true
true
10
true
11
```

### Java Ternary Operator

Java Ternary operator is used as one liner replacement for if-then-else statement and used a lot in Java programming. it is the only conditional operator which takes three operands.

Output:

```2
```

Another Example:

Output:

```5
```

### Java Assignment Operator

Java assignment operator is one of the most common operator. It is used to assign the value on its right to the operand on its left.

Output:

```14
16
```

Output:

```13
9
18
9
```

### Java Assignment Operator Example: Adding short

Output:

```Compile time error
```

After type cast:

Output:

```20
```
Next TopicKeywords in Java