This week's book giveaway is in the JavaScript forum. We're giving away four copies of Getting MEAN with Mongo, Express, Angular, and Node and have Simon Holmes on-line! See this thread for details.

Originally posted by Robbie kyodo: int y = ++x + x++; if x = 2

y=6 ++x: increase x by 1 and then evaluate x, so this expression will be of vaule 3 and x is also 3 x++: (remember x is 3 already by ++x!)evaluate x and then increase x by 1. so the expression will be of value 3 and x will be 4. acording the above explanation, both expressions are of value 3 and thus y=3+3=6.... you may try:

what's x?

Meng Yi

Robbie kyodo
Ranch Hand

Joined: May 05, 2003
Posts: 97

posted

0

int x=2; x += ++x + x++; x = 2 + 3 + 3 8 ? is it right ?

Hi Robbie In java intial evaluation order is left to right. So now evaluate ++x + x++ which gives us 3 + 3; but now makes x = 4. when first ++x is encountered it gives 3 then the value of x which is now three is now put in x, so now x also gets three, now x is incremented and now x will contain 4. So y will be 3+3=6. The evaluation order for = is right to left.

int x=2; x += ++x + x++; x = 2 + 3 + 3 8 ? is it right ?

Realize that order of operations and precedence isn't tested on the SCPJ exam. So I'd recommend concentrating more on inner classes and threads and fun stuff like that. In real life you'll use lots of ( )'s to make the precedence in your arithmatic to make things clear.

Robbie- I goofed. I was editing / deleting a post of mine in this thread, and I think the UBB got a little mixed up and it accidently deleted YOUR post instead of mine. My apologies!! - Jess

Robbie kyodo
Ranch Hand

Joined: May 05, 2003
Posts: 97

posted

0

I repost my message again

I think I mistype x =+ ++x + x++ instead of += I typed =+ it still compiles. why ?

Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270

posted

0

Originally posted by Robbie kyodo: I repost my message again

I think I mistype x =+ ++x + x++ instead of += I typed =+ it still compiles. why ?

I think for =+, it is not a compound operator, but rather a combination of operators. form maths, we know that +5 is positive five and is equal to 5 and +(-5) is still -5. so op1=+op2 is just the same as op1=op2, op1 and op2 are compatible numeric values here. something strange as y=+-5; is by all means valid in java. it is just the same as y= + (-5); and further more the same as y=-5; hope it helps.........(help myself as well ) [ May 27, 2003: Message edited by: Yi Meng ]