Explanation :
- Java operator is a symbol that is used to perform mathematical or logical manipulations.
- Java language is rich with built-in operators.
- Java operators: In any programming language (and in mathematics), we use some symbols to represent an operation (calculation).
Suppose if we want to perform an addition, then we use the symbol ” + “ Similarly when we want to perform a subtraction, then we use the symbol ” – “ These symbols are known as mathematical operators. We have different types of operators in java. Each type (group) has some symbols (operators) in it.
Java provides a rich set of operators enviroment. Java operators can be devided into following categories
Operators
|
Sign
|
Types
|
Increment & Decrements Operators
|
++ , --
|
Unary operator
|
Arithmetic
operators
|
+, -, /, %
|
Binary operator
|
Relation
operators
|
<,<=, >, >=, ==, !=
|
|
Logical
operators
|
&&, ||, !
|
|
Bitwise
operators
|
&, |. <<, >>, ~, ^
|
|
Assignment
operators
|
=, +=, -=, *=, /=, %=
|
|
Conditional
operators
|
?:
|
Ternary operator
|
Operator | Description |
---|---|
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
Increment & Decrements Operators
Operator | Description |
---|---|
++ | Increment |
−− | Decrements |
Relational Operators
Operator | Description |
---|---|
== | Is equal to |
!= | Is not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Logical Operators
Operator | Description |
---|---|
&& | And operator. Performs a logical conjunction on two expressions. (if both expressions evaluate to True, result is True. If either expression evaluates to False, result is False) |
|| | Or operator. Performs a logical disjunction on two expressions. (if either or both expressions evaluate to True, result is True) |
! | Not operator. Performs logical negation on an expression. |
Bitwise Operators
Operator | Description |
---|---|
<< | Binary Left Shift Operator |
>> | Binary Right Shift Operator |
>>> | Shift right zero fill operator |
~ | Binary Ones Complement Operator |
& | Binary AND Operator |
^ | Binary XOR Operator |
| | Binary OR Operator |
Now lets see truth table for bitwise &
, |
and ^
&
, |
and ^
a | b | a & b | a | b | a ^ b |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
The bitwise shift operators shifts the bit value. The left operand specifies the value to be shifted and the right operand specifies the number of positions that the bits in the value are to be shifted. Both operands have the same precedence.Example
a = 0001000 b= 2 a << b= 0100000 a >> b= 0000010
Assignment Operators
Operator | Description |
---|---|
= | Assign |
+= | Increments, then assigns |
-= | Decrements, then assigns |
*= | Multiplies, then assigns |
/= | Divides, then assigns |
%= | Modulus, then assigns |
<<= | Left shift and assigns |
>>= | Right shift and assigns |
&= | Bitwise AND assigns |
^= | Bitwise exclusive OR and assigns |
|= | Bitwise inclusive OR and assigns |
Misc Operators
Operator | Description |
---|---|
Conditional(Ternary) Operator ( ? : ) | Operator is used to decide which value should be assigned to the variable. |
instanceOf Operator | Object reference variables |