# Java Operators .

In this session i have discussed Java Operators .

Operators is a symbol that perform some action on the values of variable like + operator is used to perform the addition action .

Java supports following types of operators .

1-Arithmetic Operators .

2-Relational Operators .

3-Logical Operators .

4-Assignment Operators .

5-Increment and Decrement Operators .

6-Conditional Operators .

7-Bitwise Operators .

8-Instanceof Operators .

Arithmetic Operators :

The Java Arithmetic operators perform all the mathmetical and logical operation like addition , subtraction , multiplication , division etc .

 Operator Meaning Description x=20 y=10 + Plus or unary plus Add the two numbers x+y //30 - Substraction or unary minus Subtract the first number from second number x-y //10 * Multiplication . Multiplies the two numbers x*y //200 / Division. Divide the two numbers x/y // 2 % Modulo division . Return the modulus x%y // 0 .

Example :Write a java program that will add the two numbers .

``` class Hello{ public static void main(String args[]){ int x=20,y=10; int sum=x+y; System.out.println("Sum : "+sum); } } ```
```    OUTPUT : Sum : 30
```

Example : Write a java program that will subtract the two numbers .

``` class Hello{ public static void main(String args[]){ int x=20,y=10; int sub=x-y; System.out.println("Sub : "+sub); } } ```
```    OUTPUT : Sub : 10
```

Example : Write a java program that will multiplies the two numbers .

``` class Hello{ public static void main(String args[]){ int x=20,y=10; int mul=x*y; System.out.println("Mul : "+mul); } } ```
```    OUTPUT : Mul : 200
```

Example : Program that will divide the two numbers .

``` class Hello{ public static void main(String args[]){ int x=20,y=10; int div=x/y; System.out.println("Div : "+div); } } ```
```    OUTPUT : Div : 2
```

Example : Write a java program that will returns the modulus .

``` class Hello{ public static void main(String args[]){ int x=21,y=10; int mod=x%y; System.out.println("Mod : "+mod); } } ```
```    OUTPUT : Mod : 1
```

Relational Operators :

Relational operators is used to compare two quantities to take certain decision . For example you may compare the price of two items .

 Operator Meaning Description x=20 y=10 < is less than Check if the left side operand less to right side operand then condition become true x < y // true <= is less than or equal to Check if the left operand value is less than or equal to right side operand value then condition become true x<=y // true > is greater than Check if left side operand value is greater than right side operand value then condition becomes true x > y // false >= is greater than or equal to Check if left side operand value is greater then or equal to right side operand value then condition become true x>=y //false == is equal to Check if the left side operand value is same to right side of operand value then condition become true x==y // false != is not equal to Check if the left side operand value is not equal to right side of operand value then condition become true x!=y //true .

Example : Write a java program that will check the x value is greater than y value .

``` class Hello{ public static void main(String args[]){ int x=20,y=10; if(x>y){ System.out.println("x is greater than y"); } } } ```
```    OUTPUT : x is greater than y
```

Example : Write a java program that will check the condition x value is greater then or equal to y value.

``` class Hello{ public static void main(String args[]){ int x=20,y=10; if(x>=y){ System.out.println("x is greater than or equal to y"); } } } ```
```    OUTPUT : x is greater than or equal to y
```

Example : Write a java program that will check the condition x value is less then y value .

``` class Hello{ public static void main(String args[]){ int x=10,y=20; if(x<y){ System.out.println("x is less than y"); } } } ```
```    OUTPUT x is less than y
```

Example : Write a java program that will check the condtion x value is less then or equal to y value .

``` class Hello{ public static void main(String args[]){ int x=10,y=20; if(x<=y){ System.out.println("x is less than or equal to y"); } } } ```
```    OUTPUT : x is less than or equal to y
```

Example : Program that will check the condition x value is equal to y value .

``` class Hello{ public static void main(String args[]){ int x=10,y=10; if(x==y){ System.out.println("x and y values are same"); } } } ```
```    OUTPUT : x and y values are same
```

Example : Program that will check condtion x is not equal to y value .

``` class Hello{ public static void main(String args[]){ int x=10,y=20; if(x!=y){ System.out.println("x and y values are not same"); } } } ```
```    OUTPUT : x and y values are not same
```

Logical Operators :

Logical operators are used to evaluate the two or more conditions . It is combine the relational expression . You can use the any kind of expression . If condition is true then it will return 1 other wise 0 .

 Operator Meaning Description x=10 y=20 && Logical AND It return true if both operads are non zero (x==10 && y==20) //true || Logical OR It returns true if any operand is non zero (x==10 || y==10) //true ! Logical NOT It reverse the condition if operads is true then it become false !(x>y) //true .

Example : Write a java program that will check the condition if both condition is true then it will execute the statement inside the if .

``` class Hello{ public static void main(String args[]){ int x=10,y=20; if(x==10)&&(y==20){ System.out.println("Both condition true"); } } } ```
```    OUTPUT : Both condition true
```

Example : Write a java program that will check the condition if any one condition is true then the statement inside the if will execute .

``` class Hello{ public static void main(String args[]){ int x=10,y=20; if(x==10)||(y==30){ System.out.println("Single Condition true"); } } } ```
```    OUTPUT : Single Condition true
```

Example : Program that will check the condition if condtion is false then the statement inside the if will execute .

``` class Hello{ public static void main(String args[]){ int x=10,y=20; if(!(x>y)){ System.out.println("Welcome"); } } } ```
```    OUTPUT : Welcome
```

Assignment Operators :

Assignment operators are used to assign the value to variable in C# program . The usual assignment operator is = operator . C# has some short hand assignment operators as given below .

 Statement with assignment operator Statement with shorthand operator x=x+1 x+=1 x=x-1 x-=1 x=x*x x*=x x=x/(n+9) x/=n+9 x=x%b x%=b .

Example :

``` class Hello{ public static void main(String args[]){ int x=20; x=x+1; System.out.println("X : "+x); } } ```
```    OUTPUT : X : 21
```

Example :

``` class Hello{ public static void main(String args[]){ int x=20; x=x-1; System.out.println("X : "+x); } } ```
```    OUTPUT : X : 19
```

Example :

``` class Hello{ public static void main(String args[]){ int x=20; x=x*x; System.out.println("X : "+x); } } ```
```    OUTPUT : X : 400
```

Example :

``` class Hello{ public static void main(String args[]){ int x = 20 , n = 1; x = x / (n+9); System.out.println("X : "+x); } } ```
```    OUTPUT : 2
```

Example :

``` class Hello{ public static void main(String args[]){ int x = 20, b=3; x = x % b; System.out.println("X : "+x); } } ```
```    OUTPUT : 2
```

Increment & Decrement Operator :

 Operator Meaning Description x=10 y=20 ++ Increment Its increases the one value of operand x++ // 11 -- Decrement Its decreases the one value of operand y-- // 19 .

Pre-increment ++ operator

It first increases the one value and assign the increases value to variable in given expression .

Example :

``` class Hello{ public static void main(String args[]){ int x, y; x = 20; y = ++x; System.out.println("X : "+x); System.out.println("Y : "+y); } } ```
```    OUTPUT : x= 21
y= 21
```

Post-increment ++ operator

It first assigne the value to variable in given expression and then increase the one value .

Example :

``` class Hello{ public static void main(String args[]){ int x, y; x = 20; y = x++; System.out.println("X : "+x); System.out.println("Y : "+y); } } ```
```    OUTPUT : x= 21
y= 20
```

Pre-decrement -- operator

It first decreases the one value and assign the decreases value to variable in given expression .

Example :

``` class Hello{ public static void main(String args[]){ int x, y; x = 20; y = --x; System.out.println("X : "+x); System.out.println("Y : "+y); } } ```
```    OUTPUT : x= 19
y= 19
```

Post-decrement -- operator

It first assigne the value to variable in given expression and then decrease the one value .

Example :

``` class Hello{ public static void main(String args[]){ int x, y; x = 20; y = x--; System.out.println("X : "+x); System.out.println("Y : "+y); } } ```
```    OUTPUT : x= 19
y= 20
```

Conditional Operator :

Conditional Operator also known as ternary operator(?:) . It is look like below .

``` expression1 ? expression2 :expression3 ```

It uses the three operands . The first operand is used to give the condition and if the condition is true then second operand will be evaluate other wise third operand will be evaluate .

Example : Write a program that will check the maximum value among two values .

``` class Hello{ public static void main(String args[]){ int x=35; int y=20; int max=(x>y)?x:y ; System.out.println("Max : "+max); } } ```
```    OUTPUT : MAX : 35
```

Bitwise Operator :

Before read the bitwise operator please try to understand the following table .

 X Y X&Y X|Y X^Y ~X 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 0 .

 Operator Meaning Description x=12 y=25 & Binary AND operator It is used to add bit of first operand with bit of second operand if both are 1 it will be one other wise 0. x&y // 8 | Binary OR operator It is used to add bit of first operand with bit of second operand if at least one corresponding bit of two operands is 1 it will be 1 other wise 0 . x|y //29 ^ Binary XOR operator It is 1 if other two corresponding operands are opposite Please read the first table . x^y // 21 ~ Binary Ones complement operator It is change the 1 to 0 and 0 to 1 please read the first table . ~x//-13 << Left shift operator It is used to shifts all bits towards left by specified bits x<<1 //24 >> Right shift operator It is used to shifts all bits towards right by specified bits x>>1 //6 .

Bitwise AND(&) Operator :

Lets take a simple example x=12 , y=25 . First we change the values in binary format .

``` 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) & --------------------- 00001000 = 8 (In decimal) ``` ``` class Hello{ public static void main(String args[]){ int x=12; int y=25; System.out.println(x&y); } } ```
```    OUTPUT : 8
```

Bitwise OR(|) Operator :

Lets take a simple example x=12 , y=25 . First we change the values in binary format .

``` 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) | --------------------- 00011101 = 29 (In decimal) ``` ``` class Hello{ public static void main(String args[]){ int x=12; int y=25; System.out.println(x|y); } } ```
```    OUTPUT : 29
```

Bitwise XOR(^) Operator :

Lets take a simple example x=12 , y=25 . First we change the values in binary format .

``` 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) ^ --------------------- 00010101 = 21 (In decimal) ``` ``` class Hello{ public static void main(String args[]){ int x=12; int y=25; System.out.println(x^y); } } ```
```    OUTPUT : 21
```

Bitwise Complement(~) Operator :

So let 's take a look at how we get ~12 = -13: .

Convert into binary format : 00001100 Simply flip all the bits and we get: 11110011 Well, what's -13 look like in two's complement? Start with positive 13: 00001101, flip all the bits to 11110010, and add 1 to become negative value (-13), 11110011. So if you simply invert the bits in 12, you get the two's complement representation of -13.

``` class Hello{ public static void main(String args[]){ int x=12; System.out.println(~x); } } ```
```    OUTPUT : -13
```

Bitwise RightShift(<<) Operator :

Lets take a simple example x=12. First we change the values in binary format .

``` 12 = 00001100 (In Binary) --------------------- 12<<1 = 00011000 (right shift by one bit) ``` ``` class Hello{ public static void main(String args[]){ int x=12; System.out.println(x<<1); } } ```
```    OUTPUT : 24
```

Bitwise LeftShift(>>) Operator :

Lets take a simple example x=12. First we change the values in binary format .

``` 12 = 00001100 (In Binary) --------------------- 12>>1 = 00000110 (right shift by one bit) ``` ``` class Hello{ public static void main(String args[]){ int x=12; System.out.println(x>>1); } } ```
```    OUTPUT : 6
```

8-Instanceof operator

The java instanceof operator is used to test whether the object is an instance of the specified type (class or subclass or interface).

``` class Hello{ public static void main(String args[]){ Hello obj=new Hello(); System.out.println(obj instanceof Hello); } } ```
``` OUTPUT : true
```

# Welcome to AndroidATPoint.

`  `