# ++ operator

Robbie kyodo

Ranch Hand

Posts: 97

Yi Meng

Ranch Hand

Posts: 270

posted 12 years ago

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?

- 0

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

Posts: 97

Anupam Sinha

Ranch Hand

Posts: 1090

posted 12 years ago

- 0

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.

Right.

[ May 26, 2003: Message edited by: Anupam Sinha ]

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 ?

Right.

[ May 26, 2003: Message edited by: Anupam Sinha ]

Yi Meng

Ranch Hand

Posts: 270

posted 12 years ago

- 0

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.

- Jess

Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep

Robbie kyodo

Ranch Hand

Posts: 97

Yi Meng

Ranch Hand

Posts: 270

posted 12 years ago

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 ]

- 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 ]

Meng Yi

I agree. Here's the link: http://aspose.com/file-tools |