Operator is a symbol which is used to perform operation on data. Eg. +,-,/,* etc.
|
Do you know : Operator and operand |
|
Operator is a symbol which is used to perform operation on data. Operand is data on which operation is to be performed. For example,
x = 2 + 5
We have to add 2 and 5, and store the result into variable x. Here, addition and assignment are operations which are denoted by symbol + and =. Thus symbols + and = are operators.
2, 5 and x is data on which operations has to be performed. Thus 2, 5 and x are operands. |
Java operators are mainly classified on 2 bases. First, on the basis of number of operands an operator performs upon. Second, on the type of operation operator performs.
Classification based on number of operands
· Unary operators
· Binary operators
· Tertiary operators
Unary operators
Unary operators operate on one operand.
E.g. ++, --
Binary operators
Binary operators operate on two operands.
E.g. All arithmetic operators like +, - etc.
Ternary operators
Ternary operators operate on three operands.
E.g. Java has only one ternary operator also known as conditional operator.
Classification based on type of operation operator performs
· Unary operators
· Arithmetic operators
· Shift operators
· Relational operators
· Bitwise operators
· Logical operators
· Ternary / conditional operators
· Assignment operators
· Miscellaneous operators
We are going to study each operator in detail with examples. Before that lets study about in which order expression will be evaluated.
Evaluation order of expression
In an expression there may be more than one operators involved. When more than one expression has to be evaluated Java interpreter has to decide sequence of operators to be evaluated. Java makes this decision based on precedence and associatively of operator.
Java operator’s precedence and associativity
Java operator has 2 properties namely precedence and associativity.
Precedence: It is a priority order of an operator. If there are two or more operators in an expression then operator with highest precedence (priority) will be evaluated first.
For example, 2+3*5. The result would be 17, not 25 as multiplication has higher precedence than addition.
Associativity: What if all operators in expression have same precedence? That time second property of operator comes into play i.e. associativity. It tells direction of execution of operators. Direction of execution can be either left to right or right to left.
E.g. a=b=c=10. Assignment operator has associativity from right to left. Thus value 10 is assigned to c first. Then c is assigned to b and b is assigned to a.
E.g. 2+5+8. Addition operator has associativity from left to right. Thus 2 and 5 will be added first and result will be added to 8 to get final output.
Java Operators Precedence and Associativity
|
Operator Type |
Operator |
Description |
Associativity |
|
- |
[] |
Array index |
Left -> Right |
|
() |
Method call |
||
|
. |
Member access |
||
|
Unary |
++ |
Post increment |
Right -> Left |
|
-- |
Post decrement |
||
|
++ |
Pre increment |
||
|
-- |
Pre decrement |
||
|
! |
Logical NOT |
||
|
~ |
Bitwise NOT |
||
|
- |
() |
Type case |
Right -> Left |
|
- |
new |
Object creation |
|
|
Arithmetic |
* |
Multiplication |
Left -> Right |
|
/ |
Division |
||
|
% |
Modulus |
||
|
+,- |
Addition, subtraction |
||
|
- |
+ |
String concatenation |
Left -> Right |
|
Shift |
<< |
Left shift |
Left -> Right |
|
>> |
Signed right shift |
||
|
>>> |
Unsigned or zero-fill right shift |
||
|
Relational |
< <= |
Less than, less than equals |
Left -> Right |
|
> >= |
Greater than, greater than equals |
||
|
instanceof |
Reference check |
||
|
== |
Equals |
||
|
!= |
Not equals |
||
|
Bitwise |
& |
Bitwise AND |
Left -> Right |
|
^ |
Bitwise XOR |
||
|
| |
Bitwise OR |
||
|
Logical |
&& |
Logical AND |
Left -> Right |
|
|| |
Logical OR |
||
|
Ternary (Conditional) |
?: |
Conditional |
Right -> Left |
|
Assignment |
= += -= *= /= %= &= ^= |= <<= >>= >>>= |
Assignment operators |
Right -> Left |
Let’s learn operators in detail with examples:
Unary Operator
Unary operator works on single operand.
Increment Decrement operators
· Post increment/ decrement: These operators increment its operand, but evaluates to the value of that operand before it was incremented.
For example,
public class PostIncDecDemo {
public static void main(String[] args) {
int i = 10;
System.out.println(i++);
System.out.println(i);
}
}
Output:
10
11
As mentioned, it incremented value of operand but evaluated to its old value. New incremented value is reflected in next operation. Same is for post decrement.
public class PostIncDecDemo {
public static void main(String[] args) {
int i = 10;
System.out.println(i--);
System.out.println(i);
}
}
Output:
10
9
· Pre increment / decrement: These operators increment the operand and evaluates to the incremented value of that operand.
Logical NOT operator
Logical NOT operator provides opposite of current Boolean value as output.
For example
public class LogicalNotDemo {
public static void main(String[] args) {
boolean i = true;
boolean j = false;
System.out.println(!i); //false
System.out.println(!j); //true
}
}
Output:
false
true
Bitwise NOT operator
This operator inverts each bit of operand. That is converts zeros to ones and ones to zeros.
For example,
public class BitwiseNotDemo {
public static void main(String[] args) {
int i = 12;
System.out.println(~i); // ~00001010 = 11110101
}
}
Output
-13
Writing on remaining operators. Stay tuned…