About Question enthuware.ocajp.i.v8.2.927 :

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

Moderator: admin

Post Reply
heleneshaikh
Posts: 24
Joined: Wed Sep 02, 2015 3:43 am
Contact:

About Question enthuware.ocajp.i.v8.2.927 :

Post by heleneshaikh »

 k += (k = 4) * (k + 2);    
Because of the parentheses, (k=4) has a higher precedence than the assignment operator +=. So why is K not assigned the value of 4 when we reach k + ?
 k = k + (k = 4) * (k + 2);
k = 4 + (4) * (6)
k = 4 + 24
k = 4 + 24

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

Re: About Question enthuware.ocajp.i.v8.2.927 :

Post by admin »

Because before k=4 is executed the value of k is 1. So, while evaluating the expression, 1 is used as the value of the first k after =. This is how an expression is evaluated.
You might want to check with JLS to see exactly the process that is used by the JVM to evaluate an expression. Or you may also check any general computer science book for expression evaluation. (NOT required for this exam though.)
If you like our products and services, please help us by posting your review here.

goncaloncpinto
Posts: 5
Joined: Mon Feb 15, 2016 5:43 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.927 :

Post by goncaloncpinto »

This is the same as:
k = 1 + ((k = 4) * (k + 2))
k = 1 + (4 * (4 + 2)
k = 1 + (4 * 6)
k = 1 + 24
k = 25

Kevin30
Posts: 28
Joined: Sun Oct 25, 2015 10:14 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.927 :

Post by Kevin30 »

I understand the logic of the explanation.

To simplify the example:
int k = 1;
k += (k=4);

k = k + (k=4)
k = 1 + 4
k = 5

However, I don't understand the logic anymore if you add another k:
int k = 1;
k += k + (k=4);

I would think the outcome should be 9:
k = k + (k + (k=4))
k = 1 + (k + 4) // operator precedence is overridden by use of parentheses: k = 4
k = 1 + 4 + 4
k = 9

But apparently, if I run the code, the outcome is 6:
k = k + (k + (k=4))
k = 1 + (k +4) // parentheses are used, so can someone explain why k is 1 instead of 4?
k = 1 + 1 + 4
k = 6

In the example above I would think that the rule "operator precedence is overridden by use of parentheses" would ensure that the outcome would be 9 and not 6.
Could you please explain why that is not the case?

Thank you in advance!

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

Re: About Question enthuware.ocajp.i.v8.2.927 :

Post by admin »

How did you go from : k = k + (k + (k=4))
To: k = 1 + (k + 4)
It should be:
k = 1 + (1 + 4)
because the the second k is still 1 before (k=4) is executed and so that is the value that will be used to build the expression.

Had your expression been: k += (K=4) + k; then it would resolve to:
k = k + ((k=4) + k);
k = 1 + ( 4 + 4 ); //because k has been assigned 4 already by the time the last k is used.
k=9

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Kevin30
Posts: 28
Joined: Sun Oct 25, 2015 10:14 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.927 :

Post by Kevin30 »

admin wrote:How did you go from : k = k + (k + (k=4))
To: k = 1 + (k + 4)
It should be:
k = 1 + (1 + 4)
because the the second k is still 1 before (k=4) is executed and so that is the value that will be used to build the expression.
My reasoning was/is this:
I thought that the use of parentheses overrides operator precedence. So in this example "(k=4)" is being executed before any of the other code. Especially if you also take into account the rule that if you have nested parentheses, you first resolve the inner parentheses.

Thanks to your example I see that it works a bit different, but I'm not really sure why.
Your example seems to suggest that executing the code from left-to-right takes precedence over the use of parentheses. That can't be true.

So I'm probably missing something really obvious.

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

Re: About Question enthuware.ocajp.i.v8.2.927 :

Post by admin »

Parentheses go get precedence but only after the values of the variables are put in from left to right because of the rule in JLS that says "Evaluate operands before operation". This is explained in more detail here: https://docs.oracle.com/javase/specs/jl ... l#jls-15.7

I would not suggest you to spend too much time on it though because expressions can get too complicated and you are not required to evaluate expressions that are too long or too complicated in the exam. If you go through the questions of our question bank on this topic, you will be ok.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Kevin30
Posts: 28
Joined: Sun Oct 25, 2015 10:14 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.927 :

Post by Kevin30 »

Thank you! I understand it now.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 39 guests