About Question enthuware.ocajp.i.v7.2.1290 :
Moderator: admin
-
- Posts: 12
- Joined: Wed Jul 15, 2015 4:05 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1290 :
I answered correctly but only because I happened to see something similar before. I think the piece of knowledge required is not the kind of thing you find in a typical Java book, perhaps only in the Java Puzzlers book or Stackoverflow (or of course the JLS).
The explanation could have made it clearer that there is a subtle rule you have to know: in an expression like "s+= expr", the value of s to be added to the value of the expression is the value BEFORE evaluating the expression, not after. In other words, s is assigned the old value of s + the value of the expression. The language designers could have also implemented this otherwise: first evaluate the expression and then add the result to the current value of s. This really makes a difference because the expression contains "++s". If it were implemented this way, the value would be one more.
The explanation could have made it clearer that there is a subtle rule you have to know: in an expression like "s+= expr", the value of s to be added to the value of the expression is the value BEFORE evaluating the expression, not after. In other words, s is assigned the old value of s + the value of the expression. The language designers could have also implemented this otherwise: first evaluate the expression and then add the result to the current value of s. This really makes a difference because the expression contains "++s". If it were implemented this way, the value would be one more.
-
- Posts: 6
- Joined: Mon Feb 01, 2016 3:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1290 :
It seems that:
Starts to translate to:
But isn't "++s" supposed to precede the other sub-expressions which would make become as they are evaluated after?
It seems to be that variable values are resolved all at once at the beginning of the expression... still pretty confusing.
Code: Select all
s = 5;
s += s + mx(s) + ++s;
Code: Select all
s += 5 + mx(5) + 6;
Code: Select all
s + mx(s)
Code: Select all
6 + mx(6)
It seems to be that variable values are resolved all at once at the beginning of the expression... still pretty confusing.
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1290 :
No, that is not correct. While parsing the expression, s + mx(s) + ++s, you will start from left to right and substitute the values for the variables as you go forward. Now, when you encounter, ++s, you increment s and then put the new value of s in the expression. In case of s++, you put the existing value of s and then increment s. Therefore, s + mx(s) + ++s will be resolve to : 5 + mx(5) + 6. But you have something like this:olograph wrote: But isn't "++s" supposed to precede the other sub-expressions which would makebecomeCode: Select all
s + mx(s)
as they are evaluated after?Code: Select all
6 + mx(6)
s + mx(s) + s++, then that will resolve to 5 + 5 + m(5).
Important thing to note here is that in both of the following expressions, you will use 6 for the third term:
s + mx(s++) + s : 5 +mx(5) + 6
s + mx(++s) + s : 5 +mx(6) + 6
This is because s has already been incremented while working out the value of the second term i.e. mx(s++) or mx(++s).
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Wed Sep 28, 2016 6:31 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1290 :
Hi Paul!
Right now I'm a little bit confused with this piece of code:
Specifically with mx method. Because when I ran the program I added a line to the method to see the value of i in the console. And the output is:
Maybe is an stupid question but I'm really confused.
Note: I understood everything else in the excercise.
Right now I'm a little bit confused with this piece of code:
Code: Select all
static int mx(int s){
for(int i=0; i<3; i++){
s = s + i;
}
return s;
}
// and the following code snippet:
int s = 5;
s += s + mx(s) + ++s;
System.out.println(s);
- 0 // i
1 // i
2 // i
24 //Value of s
Maybe is an stupid question but I'm really confused.
Note: I understood everything else in the excercise.
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1290 :
You need to evaluate the value of s in mx step by step.
s1. You pass 5 to mx.
Loop starts: s is 5 and i is 0
s2. s = 5+0, s becomes 5 and i becomes 1
s3. s = 5+1, s becomes 6 and i becomes 2
s4. s = 6+2, s becomes 8 and i becomes 3
loop terminates because i is not < 3.
So you can see the method mx returns 8 and not 7.
HTH,
Paul.
s1. You pass 5 to mx.
Loop starts: s is 5 and i is 0
s2. s = 5+0, s becomes 5 and i becomes 1
s3. s = 5+1, s becomes 6 and i becomes 2
s4. s = 6+2, s becomes 8 and i becomes 3
loop terminates because i is not < 3.
So you can see the method mx returns 8 and not 7.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Wed Sep 28, 2016 6:31 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1290 :
Oh, I see it now. Thanks for the explanation Paul
-
- Posts: 1
- Joined: Fri Dec 16, 2016 6:05 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1290 :
Why is ++s not evaluated first in the expression(s+= s+mx(s)+ ++s) when ++ has higher precedence than +
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1290 :
A complete explanation of expression evaluation is not possible in a forum post but there are three things that you need to consider while evaluation of an expression - operator precedence, associativity, and evaluation order. In this case, ++s will be evaluated later because it occurs later in the expression.
You will need to go through a good book to understand this topic.
You will need to go through a good book to understand this topic.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Sat Feb 16, 2019 6:29 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1290 :
Sorry, but but what Ramini says is pertinent, studying is not the problem. In all books it s written that when in an expression there is the pre or post increment operator, it is the same as brackets, so it is necessary to evaluate before, independently from the position. Also Enthuware in another example say like that. In this case for example would be like that: s += s + mx(s) + (++s) . Is there any rule not clear maybe?
-
- Posts: 21
- Joined: Mon Sep 23, 2019 4:55 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1290 :
Hi team,
Thanks for the discussion on this so far. Could someone explain why the higher precedence is not a factor in this situation?
Thanks for the discussion on this so far. Could someone explain why the higher precedence is not a factor in this situation?
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1290 :
Precedence is a factor but not in the way you are thinking. Precedence determines the operand to which an operator is applied when there are two possibilities. Actual evaluation is always done from left to right. Please go through section 6.1.6 Operator precedence and evaluation of expressions of OCP Java 11 Certification Part 1 Exam Fundmentals by Hanumant Deshmukh book carefully. It explains this point in detail.
If you like our products and services, please help us by posting your review here.
-
- Posts: 21
- Joined: Mon Sep 23, 2019 4:55 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1290 :
On an additional reading of that section, I suppose the "binding" element that the book discusses comes into play. So even though '++' is of higher precedence, the operator itself is bound to 's', but only once the other operations have evaluated, since it is last from left to right. So the comment above that mentions s++ taking place multiple times does not happen. Would that be an accurate explanation?
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1290 :
Yes, that sounds about right.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 156 guests