These are the types of operators in java:
- Arithmetical operators: These are applied to perform arithmetical calculations like addition,subtraction,multiplication ,division ,modulus (Returns the remainder).
Mathematical expression: abc
Java expression: a*b*c
Unary increment and decrement operators:
These operators increase or decrease the value of an operand by 1 respectively.
Prefix:When the increment or decrement operator is applied before the operand we call it prefix operator.In this case,we change the value of the variable before the operation.
- int x=5;
x=++x; /*x increases to 6 */
So,the final value of x is 6.
- int x=5;
In this case the value of x becomes 4.
Postfix:When the increment or decrement operator is applied after the operand we call it prefix operator.In this case,we change the value of the variable after the operation.
- int x=5;
x= x++ *4;
In this case x remains 5 before multiplication.So, we get 20 stored in x.
In this case x remains 5 before multiplication.So,the final value of x is 25.
- Relational operators:As the name suggests these operators show relationship among the operands.They result in either True or False.
1.Less than (<) : Example: 3<7 Results in True
2.Greater than(>):Example: 3>7 Results in False
3.Greater than or equal to(>=):Example:6>=6 Results in True
4.Less than or equal to(<=): Example: 7<=12 Results in True
5.Equal to(==):Example: 7==7 Results in True
6.Not equal to(!=):Example: 7!=7 Results in False
3.Logical Operators: Java uses AND(&&),OR(||) or NOT(!).
Logical AND: It results in true if both expressions are true else false.
6>8 && 4!=5
This will result in false because the first condition is false.
Logical OR: It results in true if either condition is true.
6>8 || 4!=5
This will result in true because the first condition is true.
Logical NOT:It will simply revert the outcome of the expression.
This will result in true.
4.Bitwise operators: These perform operations on bit level of operands.These can be applied to byte,short,int,long and char.
Bitwise AND(&):It will give high(1)if both operands are high else low(0).
1&0 results in 0.
Bitwise OR(|):It will give high if either operand is high.
1|0 results in 1.
Bitwise complement(~):It will give the complement.
Bitwise XOR(^):It results in high if we have different value of operands and low if we have same value of operands.
1^0 results in 1.
0^0 results in 0.
Now the binary of 3 is 11.
We need to shift it left by 2 bits.
So,the final value becomes 1100 whose decimal equivalent is 12.
Now the binary of 12 is 1100.
We need to shift it right by 2 bits.
So,the final value becomes 11 whose decimal equivalent is 3.
Zero fill right shift (<<<):
Now the binary of 14 is 1110
Shift the value right by two bits and fill the shifted values with 0.So,the value comes out to
= It assigns the value like a=10;
+= Example: int a=1;a+=4 /*a=a+4*/;this results in change in value of a to 5
-= Example: int a=4;a-=1;this results in change in value of a to 3.
*= Example:int a=2;a*=3;this results in change in value of a to 6.
/= Example: int a=4;a/=1;this results in change in value of a to 4.
%= Example:int a=8;a%=3;this results in change in value of a to 2.
<<= a<<=2 is same as a=a<<2
>>= a>>=2 is same as a=a>>2
&= a&=3is same as a=a&2
Ternary operator(Conditional Assignment)
They deal with three operands.
Variable=(test expression)?value if true:value if false.
Since the expression is false so c will store value 13.