Operators in Java
What is an Operator in Java and how to use an operator?
The operator is something that can operate between two Operands and produce results. So, Java comes with a huge set of operators which can be segregated in the following way:
- Arithmetic Operators
- Relational Operators
- Bitwise Operators
- Logical Operators
- Assignment Operators
- Miscellaneous Operators
Arithmetic Operators
Arithmetic Operators are use for Mathematical operations.
Operator | Example | Description |
---|---|---|
+ | a + b | performs addition |
– | a – b | performs subtraction |
* | a * b | performs multiplication |
/ | a / b | performs division(integer) |
% | a % b | perform modulus(decimals) |
++ | a++ | increment by 1 |
— | a– | decrement by 1 |
Relational Operators
Relational operators are used for establishing the relationship between the two operands.
Operator | Example | Description |
---|---|---|
== | a==b | checks if equal |
!= | a!=b | checks if not equal |
> | a>b | checks if a is greater than b |
< | a<b | checks if b is greater than a |
>= | a>=b | checks if a is greater than or equal to b |
<= | a<=b | checks if a is lesser than or equal to b |
Bitwise Operator
Bitwise operator works on bits and performs bit-by-bit operation.Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.
If you are familiar with binary Bitwise Operations performed in Digital Computation.
For Example,
If you add 1 and 1, then it’s 10 in binary, which is obviously the binary representation of 2 in decimal form.
Operator | Example | Description |
---|---|---|
& | 1&0 gives 0 | Bitwise AND operation |
| | 1|1 gives 1 | Bitwise OR operation |
^ | 0^1 gives 0 | Bitwise XOR operation |
~ | ~1 gives 0 | Compliment operation |
>> | 010>>1 gives 001 | Right Shift operation |
<< | 010<<1 gives 100 | Left operation |
Logical Operators
This type of operator is used for generating results on the basis of a comparison of two operands.
Operator | Example | Description |
---|---|---|
&& | 1&&0 gives False | Checks operands using AND logic |
|| | 1||0 gives True | Checks operands using OR logic |
! | 1!=0 gives true | Checks operands using NOT logic |
Assignment Operator
These operators are used for assigning values to a variable, which may be conditional or non-conditional.
Operator | Example | Description |
---|---|---|
= | a = 5 stores 5 to variable a | Assigns values from right side operands to left side operand. |
+= | a += 5 adds 5 to the previously stored value of a. | It adds right operand to the left operand and assign the result to left operand |
-= | a -= 5 subtracts 5 to the previously stored value of a. | It subtracts right operand from the left operand and assign the result to left operand |
*= | a *= 5 multiplies 5 to the previously stored value of a. | It multiplies right operand from the left operand and assign the result to left operand |
/= | a /= 5 divides a by 5. | It divides left operand by the right operand and assign the result to left operand |
%= | a %= 5 performs modulus operation on a by 5 | It operates modulus operation to the left operand by the left operand and assign the result to left operand |
<<= | a <<= 10 equal to a = a << 10 | Operates left shift and then assignment operator |
>>= | a >>= 10 equal to a = a >> 10 | Operates right shift and then assignment operator |
&= | a &= 10 equal to a = a & 10 | Operates bitwise AND operation and then assignment operator |
^= | a ^= 10 equal to a = a ^ 10 | Operates bitwise XOR operation and then assignment operator |
|= | a |= 10 equal to a = a | 10 | Operates bitwise OR operation and then assignment operator |
There are a couple of more operators used in Java, which are also called Miscellaneous Operators.
Miscellaneous Operators :
- InstanceOf Operator
The operator checks, whether the object is of a particular type (class type or interface type) or not. The syntax of using it follows:
( Object reference variable ) instanceof (class/interface type)
- Ternary Operator(Conditional Operators)
This operator consists of three operands and is used to evaluate Boolean expressions. The operator decides, which value should be assigned to the variable. The syntax of it is as follows:
variable x = (expression) ? value if true : value if false
Now ,Let us have look of Precedence Table
Precedence Table
Type | Operators | Associativity |
---|---|---|
Postfix | ++, — | Left to right |
Unary | ++,- -, +, –, ~ ! | Right to left |
Multiplicative | * , /, % | Left to right |
Additive | + , – | Left to right |
Shift | << , >> , >>> | Left to right |
Relational | < ,> , <=, >=, instanceof | Left to right |
Equality | == ,!= | Left to right |
Bitwise AND | & | Left to right |
Bitwise XOR | ^ | Left to right |
Bitwise OR | | | Left to right |
Logical AND | && | Left to right |
Logical OR | || | Left to right |
Conditional | ? : | Right to left |
Assignment | = ,+=, -=, *=, /=, %=, ^= ,|=,<<= ,>>= ,>>>= | Right to left |