• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in Compound Operator Evaluation

 
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't figure out how the compound operator are evaluated. Like in this example, I thought the answer would be "s 17" but it is "s 16"
++x i.e 6 is passed to dostuff() and:
s += EASY + ++s;
would be:
EASY + ++s = 3 + 7 = 10
So, s += 10
as s is already incremented to 7, shouldn't s+= 10 be s = 7+10? i.e s = 17? Why doesn't it happen this way? What is the actual rule for evaluation?




Similarly, how is this expression evaluated?

I guessed 8.
Can anyone please explain the rules followed in these compound operations?
 
author
Posts: 23951
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

Sidharth Khattri wrote:
Similarly, how is this expression evaluated?

I guessed 8.
Can anyone please explain the rules followed in these compound operations?



Expression evaluations are done, as stated in the Java Language Specification, as follows...

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

Basically, to summarize, they are evaluated left-to-right, with side-effects taking place as soon as the operator is used (completed).

So, in this example, the first compound operator evaluates "a" to be the sum of 3 plus the result of "a" after the second compound operator completes. Granted "a" is no longer three (by the time the second compound operator completes), but that part of the expression has already been evaluated.

Henry
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s += EASY + ++s;

evaluates to s = s + EASY + ++s
= 6 + EASY + ++s
= 6 + 3 + ++s
= 9 + ++s
= 9 + 7
= 16

a += (a += c)

evaluates to
a = a + ( a+= c)
= a + ( a = a + c) // the bracket here makes all the difference.
= a + ( a = 3 + 1 )
= a + ( a =4 )
= 4 + 4
= 8


Edit : This is wrong cause s += (EASY + ++s ) ; also gives me 16. .
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Sidharth Khattri wrote:
Similarly, how is this expression evaluated?

I guessed 8.
Can anyone please explain the rules followed in these compound operations?



Expression evaluations are done, as stated in the Java Language Specification, as follows...

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

Basically, to summarize, they are evaluated left-to-right, with side-effects taking place as soon as the operator is used (completed).

So, in this example, the first compound operator evaluates "a" to be the sum of 3 plus the result of "a" after the second compound operator completes. Granted "a" is no longer three (by the time the second compound operator completes), but that part of the expression has already been evaluated.

Henry



Thanks henry. One more question if I may, is everything in java evaluated from left-to-right? I've a memory of something about right-to-left somewhere, hence I got confused.
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chan Ag wrote:s += EASY + ++s;

evaluates to s = s + EASY + ++s
= 6 + EASY + ++s
= 6 + 3 + ++s
= 9 + ++s
= 9 + 7
= 16

a += (a += c)

evaluates to
a = a + ( a+= c)
= a + ( a = a + c) // the bracket here makes all the difference.
= a + ( a = 3 + 1 )
= a + ( a =4 )
= 4 + 4
= 8



Hi Chang,
int a = 3, c = 1;
a += (a += c) //Output is: 7 not 8.

a = a + ( a+= c)
= 3 + ( a = 3 + 1) // the bracket here makes all the difference.
= 3 + ( a =4 )
= 3 + 4
= 7

Though the brackets are evaluated first, so "a" should be 4, but as the expression is already expanded as = 3 + ( a = 3 + 1) according to what Henry said, that correct answer would be 7. The brackets part can be confusing though.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I just realized that. Thanks for the correction.

I have edited the response with the correction that it's a wrong response. Please ignore it.

 
Henry Wong
author
Posts: 23951
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

Sidharth Khattri wrote:
Thanks henry. One more question if I may, is everything in java evaluated from left-to-right? I've a memory of something about right-to-left somewhere, hence I got confused.




You are probably referring to associativity. Some operators, including the compound assignment operators, do have right-to-left associativity. Remember that precedence, associativity, and order of evaluation, are three different things. And you will need to take all three into account. The example in this topic used parenthesis to not need to worry about precedence and associativity -- and allowed it to focus on evaluation order alone.

Henry
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sidharth Khattri wrote:

Henry Wong wrote:

Sidharth Khattri wrote:
Similarly, how is this expression evaluated?

I guessed 8.
Can anyone please explain the rules followed in these compound operations?



Expression evaluations are done, as stated in the Java Language Specification, as follows...

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

Basically, to summarize, they are evaluated left-to-right, with side-effects taking place as soon as the operator is used (completed).

So, in this example, the first compound operator evaluates "a" to be the sum of 3 plus the result of "a" after the second compound operator completes. Granted "a" is no longer three (by the time the second compound operator completes), but that part of the expression has already been evaluated.

Henry



Thanks henry. One more question if I may, is everything in java evaluated from left-to-right? I've a memory of something about right-to-left somewhere, hence I got confused.



Oh, now I remember, I was confusing initialization with expression evaluation. If anyone would wonder what I was talking about:

this will throw ArithmeticException and not ArrayIndexOutOfBoundsException.
And associativity - right-to-left.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I misread Henry's response earlier. So I was trying to work it until now. I re read his response and now I have the clear answer. Thanks so much.

Sorry for the confusion.
 
reply
    Bookmark Topic Watch Topic
  • New Topic