Operator precedence

R

Ryan Chan

Hi, some simple question about the Operator precedence



int i = 9;
System.out.println(i++ + 2);

Why it print 11 instead of 12, assume operator is evaluated from left
to right?
 
L

Lew

int i = 9;
System.out.println(i++ + 2);

Why it print 11 instead of 12, assume operator is evaluated from left
to right?

What is the definition of postincrement?

To put another way, given:

int i = 9;
int j = i++;

what is the value of j?

It has nothing to do with precedence.
 
M

markspace

Ryan said:
Hi, some simple question about the Operator precedence



int i = 9;
System.out.println(i++ + 2);

Why it print 11 instead of 12, assume operator is evaluated from left
to right?


Yeah, it's POST increment. That means i is incremented AFTER it's value
(9) is used. Not a precedence issue. PEBCAK issue.
 
L

Lew

Yeah, it's POST increment. That means i is incremented AFTER it's value
(9) is used. Not a precedence issue. PEBCAK issue.

OP: Consider reading the documentation.

The Java tutorial explains this sort of thing nicely:
<http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op1.html>
and, of course, the Java Language Specification is the authority on the matter:
<http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#292383>

And there's no need to assume the order of evaluation; it's specified by the
language's rules.
 
E

EJP

Why it print 11 instead of 12, assume operator is evaluated from left
to right?

Apart from what the other respondents have said, operators are not
evaluated left to right. Neither are operands other than those around a
single binary operator.
 
L

Lew

Apart from what the other respondents have said, operators are not
evaluated left to right. Neither are operands other than those around a
single binary operator.

Here is the actual rule:
<http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.7>
"The Java programming language guarantees that the operands of operators
appear to be evaluated in a specific evaluation order, namely, from left to
right."

This applies as well to operands of the ternary operator.

One should read the rest of s. 15.7 to understand the effects of things like
parentheses.
 
E

EJP

On 9/05/2010 3:20 PM, Lew wrote:

Thanks, so I should have said 'Neither are operands other than those
around a single binary or ternary operator.'
 
L

Lew

EJP said:
Thanks, so I should have said 'Neither are operands other than those
around a single binary or ternary operator.'

The operands in the expression

a + b * c / f

are evaluated left to right.

What you said is true but a bit misleading.
 
E

EJP

The operands in the expression

a + b * c / f

are evaluated left to right.

There's nothing in the JLS that actually says that. It says in effect
the operands *of an operator* are evaluated left-to-right.
 
L

Lew

There's nothing in the JLS that actually says that. It says in effect
the operands *of an operator* are evaluated left-to-right.

Of course it says that, in the quote I cited upthread (s. 15.7.1):
"The left-hand operand of a binary operator appears to be fully evaluated
before any part of the right-hand operand is evaluated."

'a' appears to be fully evaluated before '(b * c / f)'.

'b' before '(c / f)'.

'c' before 'f'.

Hence 'a' before 'b' before 'c' before 'f'.
 
E

EJP

Sorry, agreed. I was recently dealing with a claim that all operands are
evaluated left to right before any operators, and I'm still touchy about
it ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top