Page 1 of 1

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

Posted: Thu Jul 23, 2015 2:48 am
by Vermeulen
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.

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

Posted: Wed Feb 03, 2016 1:09 pm
by olograph
It seems that:

Code: Select all

s = 5;
s += s + mx(s) + ++s; 
Starts to translate to:

Code: Select all

s += 5 + mx(5) + 6; 
But isn't "++s" supposed to precede the other sub-expressions which would make

Code: Select all

s + mx(s) 
become

Code: Select all

6 + mx(6) 
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.

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

Posted: Thu Feb 04, 2016 9:58 pm
by admin
olograph wrote: But isn't "++s" supposed to precede the other sub-expressions which would make

Code: Select all

s + mx(s) 
become

Code: Select all

6 + mx(6) 
as they are evaluated after?
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:
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.

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

Posted: Wed Dec 14, 2016 1:36 pm
by vlezz94
Hi Paul!

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); 
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:
  • 0 // i
    1 // i
    2 // i
    24 //Value of s
So, if the last value of i is 2, why when I pass s to the mx method i end up with 8 instead of 7 (5 + 2)?

Maybe is an stupid question but I'm really confused.

Note: I understood everything else in the excercise.

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

Posted: Wed Dec 14, 2016 11:05 pm
by admin
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.

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

Posted: Fri Dec 16, 2016 2:27 pm
by vlezz94
Oh, I see it now. Thanks for the explanation Paul

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

Posted: Mon Jul 23, 2018 7:06 am
by ramini1996
Why is ++s not evaluated first in the expression(s+= s+mx(s)+ ++s) when ++ has higher precedence than +

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

Posted: Mon Jul 23, 2018 12:52 pm
by admin
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.

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

Posted: Tue Jun 18, 2019 3:19 am
by mario catanzariti
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?

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

Posted: Tue Nov 19, 2019 7:39 pm
by wdphipps
Hi team,

Thanks for the discussion on this so far. Could someone explain why the higher precedence is not a factor in this situation?

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

Posted: Tue Nov 19, 2019 10:18 pm
by admin
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.

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

Posted: Fri Nov 22, 2019 5:04 pm
by wdphipps
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?

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

Posted: Sat Nov 23, 2019 1:00 am
by admin
Yes, that sounds about right.