From: Lew on
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
From: EJP on
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.'
From: Lew on
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
From: Roedy Green on
On Sun, 09 May 2010 01:20:02 -0400, Lew <noone(a)lewscanon.com> 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.

--
Roedy Green Canadian Mind Products
http://mindprod.com

What is the point of a surveillance camera with insufficient resolution to identify culprits?
From: EJP on
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.
First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: Business Calendar
Next: Reading from very large file