programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Operator Precedence Question

Ioanna Katsanou
Ranch Hand
Posts: 131
1
Ok,

now for a really tricky question:

What would the above lines of code print??

As I think of it, if I hade for example

First it would do the multipication * 3 because of the parenthesis.
a = a + (30)
a = 10 + (30)
so a = 40

But in the first example

if I perform this logic is wrong !
I though that this would be the correct answer:
a=a + (a=4);
first a equals 4 because of the parenthesis
a=a + 4; and a=4
a= 4+4 =8

It seems to be wrong.

The correct answer is a=14 but I cannot seem to understand why.

Stephan van Hulst
Saloon Keeper
Posts: 7993
143
That's because in Java, operator precedence does not determine order of evaluation. Order of evaluation is always left to right.

Java will evaluate the a on the left of the addition operator before it evaluates the assignment expression on the right of the addition operator. Here are the steps that are performed:

Henry Wong
author
Sheriff
Posts: 23295
125

You are confusing operator precedence with order of evaluation.

Yes, you are correct that with the parenthesis, the assignment operator (within the parenthesis) has precedence over the add operator (outside of the parenthesis) that depends on the result of the expression within the parenthesis.

However, this doesn't change the order of evaluation. In Java, evaluation order is left to right -- obviously, using temporary variables, to hold intermediate results, if needed.

Henry

Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Expressions are evaluated left to right.

a = a + (a = 4);

To evaluate the expression on the right side of the first =:

1. take a, which is 10
2. then evaluate (a = 4), which assigns the value 4 to a, and the result of this expression is also 4
3. then take the result of step 1, which was 10, and add the result of step 2, which was 4
4. the result is 14

and finally, assign the value 14 to a.

This is interesting if you want to study the exact rules of expression evaluation in Java, but nobody should write this kind of confusing code in a serious Java application.

Ioanna Katsanou
Ranch Hand
Posts: 131
1
Stephan van Hulst wrote:That's because in Java, operator precedence does not determine order of evaluation. Order of evaluation is always left to right.

Java will evaluate the a on the left of the addition operator before it evaluates the assignment expression on the right of the addition operator. Here are the steps that are performed:

Yes but my question is the following:
why in the bellow case it would first evaluate the parenthesis, and in the above case not?

What is the difference in this scenario???

Ole Sandum
Ranch Hand
Posts: 76
3
• 1
Even though the parentheses take precedence over the plus, the expression is still evaluated from left to right.

So we have
a = 10;
a = a + (a = 4);
--evaluating from left to right gives:
a = 10 + (a = 4);
--then
a = 10 + (4); // a is now 4
--then
a = 14;

a is 14

Ole Sandum
Ranch Hand
Posts: 76
3
Ioanna Katsanou wrote:
Stephan van Hulst wrote:That's because in Java, operator precedence does not determine order of evaluation. Order of evaluation is always left to right.

Java will evaluate the a on the left of the addition operator before it evaluates the assignment expression on the right of the addition operator. Here are the steps that are performed:

Yes but my question is the following:
why in the bellow case it would first evaluate the parenthesis, and in the above case not?

What is the difference in this scenario???

In this scenario it wouldn't matter if the parentheses were evaluated first or not. It would still give the same result. In Java, though, it is evaluated from left to right.

Ole Sandum
Ranch Hand
Posts: 76
3
Ioanna Katsanou wrote:
Stephan van Hulst wrote:That's because in Java, operator precedence does not determine order of evaluation. Order of evaluation is always left to right.

Java will evaluate the a on the left of the addition operator before it evaluates the assignment expression on the right of the addition operator. Here are the steps that are performed:

Yes but my question is the following:
why in the bellow case it would first evaluate the parenthesis, and in the above case not?

What is the difference in this scenario???

To clarify. In this case it is still evaluating from left to right. That just happens to produce the same result as evaluating the parentheses first.