#### Table of Content 7

- Lecture1.1
- Lecture1.2
- Lecture1.3
- Lecture1.4
- Lecture1.5
- Lecture1.6
- Quiz1.1

# Operators

Operators are the symbols that perform the operation on values. These values are known as operands. There are three types of operators depending on the number of operands required:

**Unary Operator**: Only one operand is required.**Binary Operator**: Two operands required.**Ternary Operator**: Three operands required.

Following are the types of operator depending on the operation:

**Arithmetic Operators****Relational Operators****Logical Operators****Assignment Operators****Bitwise Operators****Conditional Operator**

**Arithmetic Operator**

- Arithmetic Operators are used to performing an arithmetic operation on the operands.
- Operands used in this can be of the numeric type or string type. Result of the arithmetic operator is always int or greater than int.

Operator | Operator Name | Description | Example |

+ | Addition | Adds two operands or do string concatenation. | a=1, b= 2 a + b = 3 OR String str=”Hello”; str+10 = Hello10 |

– | Subtraction | Subtracts | a=1,b= 2 b – a = 1 |

* | Multiplication | Multiplies | a=1,b= 2 a * b = 2 |

/ | Divide | Perform division operation | a=1,b= 2 b / a = 2 |

% | Modulus | Returns remainder | a=1,b= 3 b % a = 1 |

++ | Increment | First checks the condition, then Increase the operand value by 1 | a=1 a++; a value becomes 2 |

— | Decrement | First checks the condition, Decrease the operand value by 1 | a=2 a–; a value becomes 1 |

**Relational Operator**

- Relational operators are used to check the relation between the
**two operands**. - Result of the relational operator is always
**Boolean value**.

Here is the list of relational operators:

Operator | Operator Name | Description | Example |

== | Equal to | Returns true if two operands are equal, otherwise returns false | a=2, b=2 if(a== b) result= true |

!= | Not Equal to | Returns true if two operands are not equal, otherwise returns false | a=2, b=2 if(a== b) result=false |

< | Less than | Checks the lesser value between the two operands. | a=3, b=2 if(a<b) result=false |

> | Greater than | Checks the greater value between the two operands. | a=3, b=2 if(a>b) result= true |

<= | Less than or equal to | If the value of left operand is less than or equal to the value of right operand then it returns true. | a=23, b=2 if(a<= b) result= false |

>= | Greater than or equal to | If the value of left operand is greater than or equal to the value of right operand then it returns true. | a=22, b=2 if(a>= b) result= true |

**Logical Operator**

- Logical Operators are used to perform logical operations.
- Result of this operator is always a Boolean value.

&& | Logical AND | When Both conditions are true, the result is true otherwise false |

|| | Logical OR | When at least one condition is true, then the result is true otherwise false |

! | Logical NOT | Reverse the condition !true= false !false= true |

**Assignment Operator**

- Assignment Operators are used to assign values to the operand.

Operator | Operator Name | Description | Example |

= | Assignment | It assigns value to left-hand side operand | a = 2 It assigns 2 to a |

+= | Add then assign | It performs addition and then result is assigned to left-hand operand | a+=b means a = a + b |

-= | Subtract then assign | It performs subtraction and then result is assigned to left-hand operand | a-=b means a = a – b |

*= | Multiply then assign | It performs multiplication and then result is assigned to left-hand operand. | a*=b means a = a * b |

/= | Divide then assign | It performs division and then result is assigned to left-hand operand | a/=b means a = a / b |

%= | Modulus then assign | It performs modulus and then result is assigned to left-hand operand | a%=b means a = a % b |

**Bitwise Operators**

- Bitwise Operators are used to perform operations on individual bits.

<<= | Left shift AND assignment operator | It performs Binary left shift and then result is assigned to left-hand operand | a=5; a>>=7 means 7 times left shift, then result to a |

>>= | Right shift AND assignment operator | It performs Binary right shift and then result is assigned to left-hand operand | a=5; a>>=7 means 7 times right shift, then result to a |

&= | Bitwise AND assignment operator | It performs bitwise AND then result is assigned to left-hand operand | a=5; a&=7 means bitwise AND operation, then result to a |

^= | bitwise exclusive OR and assignment operator | It performs bitwise exclusive OR and then result is assigned to left-hand operand | a=5; a^=7 means bitwise XOR operation, then result to a |

<< | Left shift operator | It performs Binary left shift | a<<1 means one bit is left-shifted |

>> | Right shiftoperator | It performs Binary right shift | a>>1 means one bit is right-shifted |

& | Bitwise AND | It performs bitwise AND | 5 & 7 means binary XOR of 5 and 7 |

^ | bitwise exclusive OR | It performs bitwise exclusive OR | 5^7 means binary XOR operation on 5 and 7 |

| | bitwise inclusive OR | It performs bitwise inclusive OR | 2|1 means 2 and 1 binary OR operation |

**Conditional Operator**

- It is a ternary operator.

Syntax: **operand1? operand 2:operand3;**

- Both the operand must of Boolean type.
- If true, returns operand2, otherwise returns operand3.

Example

boolean I = 3>2?true:false; ->returns true.

**new Operator**

new operator is used to create objects for class.

Example:

class BbsLearn{

public static void main(String args[]){

Test t=new Test(); // t object is created

}

}

Here, t object will be created in the main memory so that we can access the members of the Test class.

**instanceof Operator**

It is used to check whether the given object belongs to a specified class or not. It is also called Type Comparision Operator. It returns a Boolean value.

**Syntax: ****<reference variable>instanceof<classname>**

Example:

class BbsLearn{

public static void main(String args[]){

Test t=new Tets(); // t object is createdSystem.out.println(t instanceof Object); // returns true

}

}

**Output:**

**true**

**Operators Precedence**

Operator precedence means the priorities of operators.

Say you have given an equation 10-2*2. How will you know whether to subtract first or multiply? To solve this confusion, operator precedence has provided.

First, 2*2 will be performed according to the below table then, the result will be multiplied by 10. The answer will be 6.

Category | Operator | Associativity |

Postfix | () [] . (dot operator) | Left to right |

Unary | ++ – – ! ~ | Right to left |

Multiplicative | * / % | Left to right |

Additive | + – | Left to right |

Shift | >>>>><< | Left to right |

Relational | >>= <<= | Left to right |

Equality | == != | Left to right |

Bitwise AND | & | Left to right |

Bitwise XOR | ^ | Left to right |

Bitwise OR | | | Left to right |

Logical AND | && | Left to right |

Logical OR | || | Left to right |

Conditional | ?: | Right to left |

Assignment | = += -= *= /= %=>>=<<= &= ^= |= | Right to left |

Comma | , | Left to right |