• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Order of Operators' Evaluation

 
Ranch Hand
Posts: 46
Eclipse IDE Slackware Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having a hard time deciphering how the expression is evaluated when various operator groups are combined (can't figure out the associativity), for instance:

In above code, a is output as "7" and b as "4"! How do we get here? How come the value of "b" is all of sudden altered to "4" (5 - 1 but how)? However, the following modification in order of operators -- from "+=--" to "+=+-") would yield a different result:

Which makes sense because I believe the expression above is expanded to "a = 3 + +(-5) = -2", but the former just doesn't make much sense.

I know the associativity of "+" and "-" works from left-to-right and "+=" from right-to-left.
 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yalvin Duha wrote:I'm having a hard time deciphering how the expression is evaluated when various operator groups are combined (can't figure out the associativity), for instance:

In above code, a is output as "7" and b as "4"! How do we get here? How come the value of "b" is all of sudden altered to "4" (5 - 1 but how)?



In above scenario assignment operator(+=) has lower precedence than decrement operator(--).

Yalvin Duha wrote:
However, the following modification in order of operators -- from "+=--" to "+=+-") would yield a different result:

Which makes sense because I believe the expression above is expanded to "a = 3 + +(-5) = -2", but the former just doesn't make much sense.

I know the associativity of "+" and "-" works from left-to-right and "+=" from right-to-left.



And in this scenario also assignment operator(+=) has lower precedence than additive operator(+/-). There is no such operator like +- or -+ ,so it's just to make confuse,so you can think code like this

 
Yalvin Duha
Ranch Hand
Posts: 46
Eclipse IDE Slackware Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:

Yalvin Duha wrote:I'm having a hard time deciphering how the expression is evaluated when various operator groups are combined (can't figure out the associativity), for instance:

In above code, a is output as "7" and b as "4"! How do we get here? How come the value of "b" is all of sudden altered to "4" (5 - 1 but how)?



In above scenario assignment operator(+=) has lower precedence than decrement operator(--).


You are right. I even mentioned it that the precedence works from right-to-left, and of course the unary operators are evaluated prior to assignment operators. I got thrown off by "+-" or "-+", eclipsing the fact that they are indeed unary operators ("--" and "++") and not additives, i.e. "- and -" or "+ and +". On second thought, I don't believe these precedence questions are in the test, right?

Thank you.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yalvin Duha wrote:
You are right. I even mentioned it that the precedence works from right-to-left, and of course the unary operators are evaluated prior to assignment operators. I got thrown off by "+-" or "-+", eclipsing the fact that they are indeed unary operators ("--" and "++") and not additives, i.e. "- and -" or "+ and +". On second thought, I don't believe these precedence questions are in the test, right?

Thank you.



Yes they're unary operators here,and precedence questions are not important from test point of view.
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Subject of this topic wrote:Order of Operators' Evaluation



Yalvin Duha wrote:I'm having a hard time deciphering how the expression is evaluated when various operator groups are combined (can't figure out the associativity), for instance:

Which makes sense because I believe the expression above is expanded to "a = 3 + +(-5) = -2", but the former just doesn't make much sense.

I know the associativity of "[b]+" and "-" works from left-to-right and "+=" from right-to-left[/b].



saloni jhanwar wrote:
In above scenario assignment operator(+=) has lower precedence than decrement operator(--).



This topic seems to mix "precedence", "associativity", and "order of evaluation".... I understand that this topic needs to address all three, but you need to understand that it is actually "three" -- meaning that precedence and associativity tables isn't enough.

To deal with order of evaluation, you need to understand section 15.7 of the Java Language Specification...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7

Henry
 
Yalvin Duha
Ranch Hand
Posts: 46
Eclipse IDE Slackware Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

This topic seems to mix "precedence", "associativity", and "order of evaluation".... I understand that this topic needs to address all three, but you need to understand that it is actually "three" -- meaning that precedence and associativity tables isn't enough.

To deal with order of evaluation, you need to understand section 15.7 of the Java Language Specification...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7


As always, you point to the right direction. Very helpful.

Thank you.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the code below:

int m1(int i) {System.out.println(i + ","); return i;}

void test9()
{
int i = 0;
i = i++ + m1(i); System.out.println(i);
}
Output is:
1,
1

I check the operator precedence in this website:
http://bmanolov.free.fr/javaoperators.php

I expected the output to be 0,1.
does i++ evaluated before calling method m1()?
Can anyone help?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic