Velocity Reviews > Java > Operator precedence

# Operator precedence

Ryan Chan
Guest
Posts: n/a

 05-08-2010
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?

Lew
Guest
Posts: n/a

 05-08-2010
On 05/08/2010 09:48 AM, Ryan Chan wrote:
> 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.

--
Lew

markspace
Guest
Posts: n/a

 05-08-2010
Ryan Chan wrote:
> 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.

Lew
Guest
Posts: n/a

 05-08-2010
Ryan Chan wrote:
>> int i = 9;
>> System.out.println(i++ + 2);
>>
>> Why it print 11 instead of 12, assume operator is evaluated from left
>> to right?

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

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.

--
Lew

EJP
Guest
Posts: n/a

 05-09-2010
On 8/05/2010 11:48 PM, Ryan Chan wrote:
> 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.

Lew
Guest
Posts: n/a

 05-09-2010
On 05/09/2010 12:51 AM, EJP wrote:
> On 8/05/2010 11:48 PM, Ryan Chan wrote:
>> 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.

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.

--
Lew

EJP
Guest
Posts: n/a

 05-09-2010
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.'

Lew
Guest
Posts: n/a

 05-10-2010
EJP wrote:
> 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.

--
Lew

Roedy Green
Guest
Posts: n/a

 05-10-2010
On Sun, 09 May 2010 01:20:02 -0400, Lew <(E-Mail Removed)> wrote,
quoted or indirectly quoted someone who said :

>"The Java programming language guarantees that the operands of operators
>appear to be evaluated in a specific evaluation order, namely, from left to
>right."

see http://mindprod.com/jgloss/precedence.html

There are left and right associations.

--
http://mindprod.com

What is the point of a surveillance camera with insufficient resolution to identify culprits?

EJP
Guest
Posts: n/a

 05-11-2010
On 10/05/2010 2:33 PM, Lew wrote:
> EJP wrote:
>> 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.

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