About Question enthuware.ocajp.i.v8.2.927 :
Moderator: admin
-
- Posts: 24
- Joined: Wed Sep 02, 2015 3:43 am
- Contact:
About Question enthuware.ocajp.i.v8.2.927 :
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
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
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.927 :
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.)
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.
-
- Posts: 5
- Joined: Mon Feb 15, 2016 5:43 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.927 :
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
k = 1 + ((k = 4) * (k + 2))
k = 1 + (4 * (4 + 2)
k = 1 + (4 * 6)
k = 1 + 24
k = 25
-
- Posts: 28
- Joined: Sun Oct 25, 2015 10:14 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.927 :
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!
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!
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.927 :
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.
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.
-
- Posts: 28
- Joined: Sun Oct 25, 2015 10:14 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.927 :
My reasoning was/is this: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.
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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.927 :
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.
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.
-
- Posts: 28
- Joined: Sun Oct 25, 2015 10:14 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.927 :
Thank you! I understand it now.
Who is online
Users browsing this forum: No registered users and 105 guests