Page 1 of 1

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

Posted: Wed Sep 23, 2015 3:50 am
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

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

Posted: Wed Sep 23, 2015 11:27 am
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.)

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

Posted: Mon Feb 15, 2016 6:06 am
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

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

Posted: Sat May 20, 2017 7:59 am
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!

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

Posted: Sat May 20, 2017 11:23 pm
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.

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

Posted: Sun May 21, 2017 4:42 am
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.

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

Posted: Sun May 21, 2017 5:39 am
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.

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

Posted: Sun May 21, 2017 9:41 am
by Kevin30
Thank you! I understand it now.