About Question enthuware.ocajp.i.v7.2.1238 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
boonnick
Posts: 9
Joined: Fri Jan 31, 2014 8:26 pm
Contact:

About Question enthuware.ocajp.i.v7.2.1238 :

Post by boonnick »

Regarding
This becomes (remember that k = 1 at this point): k = 1 + 3 + (++k) i.e. k = 1 + 3 + 2; (at this point value of k is 2 because of ++k). But the value of Right Hand Side has not yet been assigned to k. k = 6; 6 is assigned to k thereby overwriting the value of 2.
Why doesn't it equate to k = 2 + 3 + 2 if k is incremented?

Cheers.

admin
Site Admin
Posts: 10398
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1238 :

Post by admin »

Because k is 1 before the evaluation reaches ++k.

shining_dragon
Posts: 14
Joined: Sat Mar 01, 2014 9:12 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1238 :

Post by shining_dragon »

but isn't it pre-increment's associativity is Right to Left?

I was expecting ++k will be evaluated first since it is of higher precedence compared to arithmentic ( addition ) and assignment (+=):
1. ++k is evaluated first since it has the highest precedence of them all and its associativity is R-L. Thus k = 2.
2. K+3 becomes 2 + 3 = 5 (>>>k is still 2)
3. 5 + 2 = 7.

So why not 7??

admin
Site Admin
Posts: 10398
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1238 :

Post by admin »

To get a clear understanding of evaluation order, you need to go through this: http://docs.oracle.com/javase/specs/jls ... l#jls-15.7

HTH,
Paul.

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests