# operators in java

• An operator is basically a symbol or token,which performs arithmethical or logical operations and gives meaningful result.
• There are following type of operator in java-
1. Arithmetical Operator
2. Relational Operator
3. Logical Operator
4. Bitwise Operator
5. Assignment Operator
6. Conditional Operator

## Arithmetic Operator

• The operator,which are applied to perform arithmetical calculation in a program,are known as Arithmetical Operations.
• Arithmmetic operator are of 3 type-
1. Unary Operator
2. Binary Operator
3. Ternary Operator
• Unary Operator-An arithmetical operator ,which is applied with a single operand is known as Unary Operator.Example-+,-,++,--
• Binary Operator-An arithmetical operator,which deals with two operands, is known as Binary Operator.Example-+,-,*,/ and % etc
 Operators Description Addition(+) Add value on either side of operator Subtraction(-) Subtracts right-hand operand from left-hand operand. Multiplication(*) Multiplies values on either side of the operator. Division(/) Divides left-hand operand by right-hand operand. Increment(++) Increases the value of operand by 1. Decrement(--) Decreases the value of operand by 1. Modulus(%) Divides left-hand operand by right-hand operand and returns remainder.

## Conditional Operator or Ternary Operator

Ternary Operator deal with three operands.It is also called conditional assignment statement because the value assigned to a variable depends upon a logical expression.

### Example

a=5,b=3;

max=(a>b)?a:b;

Here,the value 5 is stored in max as a>b is true

``````// Program using ternary operator

``````
```public class TernaryOperator {
public static void main(String args[]){
int num1 , num2;
num1 = 10;
num2 = (num1 == 1) ? 20: 30;
System.out.println( "Value of num2 is : " +  num2 );
num2 = (num1 == 10) ? 20: 30;
System.out.println( "Value of num2 is : " + num2 );
}
}
```

### Output

```Value of num2 is : 30
Value of num2 is : 20```

## Relational Operator

These operator are used to show the relationship among the operands.Relational Operators compare the values of the variable and result in terms of 'True' or 'False'(i.e 0 or 1).

 Operator Description Format Result if a=10,b=6; < Less than a Greater than a>b true <= Less than or equal to a<=b false >= Greater than or equal to a>=b true == Equal to a==b false != Not equal to a!=b true

## Logical Operator

Java uses logical operators AND(&&),OR(||) or NOT(!).These operators yield 1 or 0 depending upon the outcome of different expressions.

 Operator Symbol Format AND && (a>b)&&(a>c) OR || (a==b)||(a==c) NOT ! !(a==b)

## Bitwise Operator

In java programming,you can use some special types of operators,which perform operations on bit level of the operands.These operators use byte,short int and long type operands.

 Operator Description | Bitwise OR & Bitwise AND ~ Bitwise Complement ^ Bitwise XOR << Left Shift >> RIght Shift >>> Unsigned Right Shift

### Example

``````// Program to perform bitwise operation

public class bitwiseoperation{
public static void main(String args[])
{
int a,b;
a=12;b=10;
```int n = 35,m;
```
``````m=~n;
System.out.println("complement is ="+m);
System.out.println("(a&b)="+(a&b));
System.out.println("(a|b)="+(a|b));
System.out.println("(a^b)="+(a^b));
System.out.println("(a<<b)="+(a<<b));
System.out.println("(12>>2)="+(12>>2));
System.out.println("(14>>>2)="+(14>>>2));
}
}``````

### Output

```complement is =-36
```(a&b)=8
(a|b)=14
(a^b)=6
(a<<b)=48
(a>>b)=3
(a>>>b)=3``````

## Assignment Operator

Assignment operators are used in Java to assign values to variables.

 Operator Description Example = Assigns values from right side operands to left side operand. a=b+c,a will assign addition of a and b += It adds right operand to the left operand and assign the result to left operand. a+=b is equal to a=a+b -= It subtracts right operand from the left operand and assign the result to left operand. a-=b equal to a=a-b *= It multiplies right operand with the left operand and assign the result to left operand. a*=b, equal to a=a*b /= It divides left operand with the right operand and assign the result to left operand. a/=b,a/ is equal to a=a/b %= It takes modulus using two operands and assign the result to left operand. a%= b is equal to a =a %b <<= Left shift AND assignment operator. a <<= 2 is same as a = a<< 2 >>= Right shift AND assignment operator. a >>= 2 is same as a = a >> 2 &= Bitwise AND assignment operator. a &= 2 is same as a = a & 2 ^= Bitwise exclusive OR and assignment operator. a ^= 2 is same as a=a ^ 2 |= Bitwise inclusive OR and assignment operator. a |= 2 is same as a = a | 2