Types of Operator in Java?
- It is a special symbol which is used to perform logical or mathematical operation on data or variable.
Operand
- It is a data or variable on which the operation is to be performed.
Types of Operator
- ⇒Arithmetic Operators
- ⇒Relational Operators
- ⇒Logical Operators
- ⇒Assignment Operators
- ⇒Bitwise Operators
- ⇒Increment/Decrement Operators
- ⇒Conditional Operators
Arithmetic Operators
Symbol | Operation | Example |
+ | Addition | x+y |
- | Subtraction | x-y |
* | Multiplication | x*y |
/ | Division | x/y |
% | Modulus | x%y |
class Easy { public static void main(String[] args) { int a=5,b=3; System.out.println("Add="+(a+b)); System.out.println("Sub="+(a-b)); System.out.println("Multi="+(a*b)); System.out.println("Div="+(a/b)); //Note:-modulus(%) always holds remainder value System.out.println("Mod="+(a%b)); } } /* ### Output ### Add=8 Sub=2 Multi=15 Div=1 Mod=2 */
Relational Operators
Symbol | Operation | Example |
== | Equal to | 2==3 returns 0 |
!= | Not equal to | 2!=3 returns 1 |
> | Greater than | 2>3 returns 0 |
< | Less than | 2<3 returns 1 |
>= | Greater than or equal to | 2>=3 returns 0 |
<= | Less than or equal to | 2<=3 returns 1 |
Logical Operators
(x>y)&&(x>z) | Here this expression returns true if both conditions are true. |
(x>y)||(x>z) | Here this expression returns true if any one or both conditions are true. |
!(x>y) | Not operator reverses the state means if the condition is true it returns false and if the condition is false it returns true. |
class Easy { public static void main(String[] args) { int a=10,b=60,c=40; if(a>b&&a>c) System.out.println("a is greatest"); if(b>a&&b>c) System.out.println("b is greatest"); if(c>a&&c>b) System.out.println("c is greatest"); } } /* ### Output ### b is greatest */
Assignment Operators
Symbol | Example | Same as |
= | x=y | x=y |
+= | x+=y | x=x+y |
-= | x-=y | x=x-y |
*= | x*=y | x=x*y |
/= | x/=y | x=x/y |
%= | x%=y | x=x%y |
class Easy { public static void main(String[] args) { int x1=5,y1=3; x1+=y1;//x1=x1+y1 System.out.println(x1); int x2=5,y2=3; x2-=y2;//x2=x2-y2 System.out.println(x2); int x3=5,y3=3; x3*=y3;//x3=x3*y3 System.out.println(x3); int x4=5,y4=3; x4/=y4;//x4=x4/y4 System.out.println(x4); int x5=5,y5=3; x5%=y5;//x5=x5%y5 System.out.println(x5); } } /* ### Output ### 8 2 15 1 2 */
Bitwise Operators
Symbol | Operation | Example |
& | Bitwise AND | x&y |
| | Bitwise OR | x|y |
<< | Shift Left | x<<2 |
>> | Shift Right | x>>2 |
^ | X-OR | x^y |
class Easy { public static void main(String[] args) {//variable declaration int a=5,b=3,c; c=a&b;//AND Operation System.out.println("a&b="+c); c=a|b;//OR Operation System.out.println("a|b="+c); c=a>>2;//Shift right Operation System.out.println("a>>2="+c); c=a<<2;//Shift left Operation System.out.println("a<<2="+c); c=a^2;//X-OR Operation System.out.println("a^2="+c); } } /* ### Output ### a&b=1 a|b=7 a>>2=1 a<<2=20 a^2=7 */
Increment/Decrement Operators
Symbol | Name | Function | Example |
++ | Increment | It increments the value by 1 | ++x |
-- | Decrement | It decrements the value by 1 | --x |
class Easy { public static void main(String[] args) {//variable declaration int a=5,b=10; System.out.println(++a); System.out.println(--b); } } /* ### Output ### 6 9 */
Conditional Operators
- If the condition is true second part will execute otherwise third part will execute.
class Easy { public static void main(String[] args) {//variable declaration int a=5,b=10,max; max=a>b?a:b; //don't be confused,here + is separator in java System.out.println("Greater value is "+max); } } /* ### Output ### Greater value is 10 */