Operators in java
Operator is used to perform some operation.In java programming there are seven operators available.
Types of operator
- Arithmetic operator
- Relational operator
- Assignment operator
- Boolean Logiacal operator
- Bitwise operator
- Unary operator
- Ternary operator
1. Arithmetic operator
Arithmetic operator used to perform the basic mathematics operation.
Operator | Example | Explain |
---|---|---|
+ | x+y | Add the number |
- | x-y | Subtract the number |
* | x*y | multiplying the number |
/ | x/y | dividing the number |
% | x%y | remainder of the number |
public class Main { public static void main(String[] args) { int x = 5; int y = 4; System.out.println(x+y); } }
Output
9
2. Relational operator
It is also called the comparison operator.It used to compare the value.
Operator | Example | Explain |
---|---|---|
== | x==y | To compare the x and y values are equal. |
!= | x!=y | To compare the x and y values are not equal. |
< | x<y | To compare x values is greater than y values. |
> | x>y | To compare x values is lesser than y values. |
<= | x<=y | To compare x values is greater than or equal in y values. |
>= | x>=y | To compare x values is lesser than or equal in y values. |
public class Main { public static void main(String[] args) { int x = 5,y = 4; System.out.println(x==y); System.out.println(x!=y); System.out.println(x<y); System.out.println(x>y); System.out.println(x<=y); System.out.println(x>=y); } }
output
false true false true false true
3. Assignment operator
Assignment operator used to assign the value.But it's assign the value like , if x+=5 means x=x+5.
Operator | Example | How assign |
---|---|---|
+= | x+=10 | x=x+10 |
-= | x-=10 | x=x-10 |
*= | x*=10 | x=x*10 |
/= | x/=10 | x=x/10 |
%= | x%=10 | x=x%10 |
&= | x&=10 | x=x&10 |
|= | x|=10 | x=x|10 |
^= | x^=10 | x=x^10 |
>= | x>>=10 | x=x>>10 |
<<= | x<<=10 | x=x<<10 |
public class Main { public static void main(String[] args) { int x = 1; x += 10; //x=x+10 => 1+10 System.out.print(x); //x=11 } }
Output
11
4. Boolean Logical operator
It used to perform the logical operation between two or more condition.
Operator | Example | Explain |
---|---|---|
&& | (condition1) && (condition2) | AND(&&) operator will become true , when all the conditions are true. |
|| | (condition1) || (condition2) | OR(||) operator will become true , when any one condition is true. |
! | ! (condition) | NOT(!) operator perform the opposite of the condition. |
AND( && ) operator
public class Main { public static void main(String[] args) { int x=5,y=6; if((x<10)&&(x<y)) { System.out.println("Both are true"); } } }
Output
Both condition are true
OR( || ) operator
public class Main { public static void main(String[] args) { int x=5,y=6; if((x==10)||(x<y)) { System.out.println("condition true"); } } }
Output
condition true
NOT( ! ) operator
public class Main { public static void main(String[] args) { int x=5; if(!(x==10)) { System.out.println("condition true"); } } }
Output
condition true
5. Bitwise operator
Bitwise operator used to perform the Logical operation and shift operation in a binary code.
Operator | Example | Explain |
---|---|---|
& | x&y | To perform the AND operation of x,y binary code. |
| | x|y | To perform the OR operation of x,y binary code. |
^ | x^y | To perform the XOR operation of x,y binary code. |
<< | x<<y | Left shift operator to perform the add the y number of 0's in x binary code in last. |
>> | x>>y | Right shift operator to perform the remove the y number of binary code in last of x binary code. |
>>> | x>>>y | Right shift filled with zero operator to perform the remove the last y number digit in x binary number. |
AND(&) operator:
public class Main { public static void main(String[] args) { int x=5; // x=0 1 0 1 int y=6; // y=0 1 1 0 int z=x&y; // z=0 1 0 0 System.out.print(z); } }
Output
4
OR(|) operator:
public class Main { public static void main(String[] args) { int x=5; // x=0 1 0 1 int y=6; // y=0 1 1 0 int z=x|y; // z=0 1 1 1 System.out.print(z); } }
Output
7
XOR(^) operator:
public class Main { public static void main(String[] args) { int x=5; // x=0 1 0 1 int y=6; // y=0 1 1 0 int z=x^y; // z=0 0 1 1 System.out.print(z); } }
Output
3
Leftshift(<<) operator:
public class Main { public static void main(String[] args) { int x=5; // x=0 1 0 1 int y=x<<2; // y=0 1 0 1 0 0 System.out.print(y); } }
Output
20
Rightshift(>>) operator:
public class Main { public static void main(String[] args) { int x=5; // x=0 1 0 1 int y=x>>2; // y=0 1 System.out.print(y); } }
Output
1
Right shift filled with zero(>>>):
public class Main { public static void main(String[] args) { int x=8; // x=1 0 0 0 int y=x>>>2; // y=1 0 System.out.print(y); } }
Output
2
6. Unary operator
Unary operator used to increment or decrement the value.It's mostly using , looping and recursion function. It's increment/decrement the value only one by one.
Operator | Example | Explain |
---|---|---|
prefix Increment | ++x | It's increment the value. |
prefix Decrement | --x | It's decrement the value. |
postfix Increment | x++ | It's assign the value.But Not increment |
postfix Decrement | x-- | It's assign the value.But not decrement |
public class Main { public static void main(String[] args) { int x=5; System.out.println(x++); System.out.println(x); } }
Output
5 6
public class Main { public static void main(String[] args) { int x=5; System.out.println(++x); System.out.println(x); } }
Output
6 6
public class Main { public static void main(String[] args) { int x=5; System.out.println(x--); System.out.println(x); } }
Output
5 4
public class Main { public static void main(String[] args) { int x=5; System.out.println(--x); System.out.println(x); } }
Output
4 4
7. Ternary operator
Ternary operator to perform like if else statement. "operand 1" is a condition.if condition is true ,"operand 2" will be execute , otherwise "operand 3" will be execute.
Syntax:
operand1 ? operand2 : operand3
public class Main { public static void main(String[] args) { int m=5,n=6; System.out.print(m<n?"n is greater":"m is greater"); } }
Output
n is greater