Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Operator precedence

Reply
Thread Tools

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?

 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
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.

 
Reply With Quote
 
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.


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.

--
Lew
 
Reply With Quote
 
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.
 
Reply With Quote
 
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
 
Reply With Quote
 
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.'
 
Reply With Quote
 
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
 
Reply With Quote
 
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.

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

What is the point of a surveillance camera with insufficient resolution to identify culprits?
 
Reply With Quote
 
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
operator precedence with && and comma siliconwafer C Programming 21 08-10-2005 12:27 AM
operator= and implicit type conversion - precedence. Master of C++ C++ 4 02-07-2005 09:28 AM
Operator Precedence kbd Java 10 07-27-2004 03:50 AM
Operator precedence David Frauzel Perl 2 05-16-2004 11:19 PM
&& and || Operator precedence enforcement Richard Bos C Programming 18 01-20-2004 01:04 PM



Advertisments