First of all, let’s understand two main things,
- Operator: In computer programming, an operator is an object that is capable of manipulating a value. For example, 3 + 5, here ‘+’ is the operator. Basically, an operator is something that performs an operation.
- Operand: In computers, an operand is the part of a computer instruction that specifies data that is to be operating on. For example, 3 + 5, here 3 and 5 are the operands. Basically, an operand is something upon which any operation is performed on.
Now that you’ve got the basic grip on what an Operand and
Operator is, let’s discuss about operator precedence.
What is a precedence level?
Suppose, you’re a minister and you’re with the prime minister. Now in case of leaving a meeting, you’ll let prime minister go first, then you’ll go. But why?
It’s simple. Because the prime minister is more important than you are (no offense here!)
Same thing is with the operators.
I assume we’ll learned the BODMAS rule back in the high school, right?
If we’ve got this,
3 + 8 / 4
What is a precedence level?
Suppose, you’re a minister and you’re with the prime minister. Now in case of leaving a meeting, you’ll let prime minister go first, then you’ll go. But why?
It’s simple. Because the prime minister is more important than you are (no offense here!)
Same thing is with the operators.
I assume we’ll learned the BODMAS rule back in the high school, right?
If we’ve got this,
3 + 8 / 4
What to do?
- Firstly, we’ll divide 4 by 5
- Then we’ll add up 3 with 2
- Thus, we got the result, 5!
Now, here the ‘+’ and ‘/’ are the operators and 3,8,4
are the operands.
Now we performed the dividing
operation first, because according to BODMAS is has higher priority than
addition.
This simple higher priority concept is called the Operator Precedence in Java.
Here is the list of the precedence of the operators in Java.
This simple higher priority concept is called the Operator Precedence in Java.
Here is the list of the precedence of the operators in Java.
Now that you’ve understood (hopefully) how operator precedence works, there’s this little term called Associativity.
Let’s look at this following
example,
3 + 4 + 5 + 5
Now all the operators on the
preceding example is ‘+’.
Now, normally what you’d want to do in your head is to calculate all the numbers at once and get the result.
Computers, however, are not so smart!
Now, normally what you’d want to do in your head is to calculate all the numbers at once and get the result.
Computers, however, are not so smart!
There are these + operators are among the numbers.
This + is essentially a Binary Additive operator.
The Additive part is easy to understand, as it only adds its operands.
Now it’s called Binary operators because it deals with two operands at once!
So the first + operator will add just 3 and 4. And it makes sense intuitively.
The - , / and *, all these operators work in the same way.
They all deal with two operands at once!
Now the associativity part comes into play.
When all the operators are of same precedence level (take the example above for illustration) Associativity tells us from which way the operators will start working on.
For,
3 + 4 + 5 + 5
The computer will solve it as
follows,
3 + 4 + 5 + 5
=7 + 5 + 5
=12 + 5
=17
=7 + 5 + 5
=12 + 5
=17
See, how this works from left to
right?
This is associativity.
The associativity for the Additive operator is Left to Right.
This is associativity.
The associativity for the Additive operator is Left to Right.
So, here’s the rule of thumb,
- First comes Operator Precedence Level
- If and only if all the operators are of same Precedence Level, then comes associativity.
Let’s evaluate another example,
3 + 5 + 8 / 2 + 5 * 2
3 + 5 + 8 / 2 + 5 * 2
First, as the / and * operators are of
higher precedence level than the + operator, this two will be calculated
first.
But then again / and * operators are of same precedence level as well. So, associativity comes into play!
The leftmost operator, in this case, the / operator will be calculated first. So,
But then again / and * operators are of same precedence level as well. So, associativity comes into play!
The leftmost operator, in this case, the / operator will be calculated first. So,
=3 + 5 + 4 + 5 * 2
Now, the * operator will be calculated as it’s of
higher precedence level than the + operator.
=3 + 5 + 4 + 10
Now again all the operators are of same precedence level.
So, associativity comes into play once more! The rest is simple!
=8 + 4 + 10
=12 + 10
=24
Now, lets look at the following line of code,
x++;
No comments