Page 1 of 1

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

Posted: Sat Mar 09, 2013 8:33 am
by The_Nick
Hi,
I thoroughly understand why it prints out 2, therefore the whole logic thing of the difference between short circuit and not. However I would like to know why "method1(i++) " actually increments i.
Aren't primitive passed by value? passed a copy of the value? Why does the increment apply to the original copy?
Thanks a lot in advance.

The_Nick

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

Posted: Sat Mar 09, 2013 8:41 am
by admin
Because increment is not being done in method1. It is done in the place where the method is called.

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

Posted: Sun Dec 15, 2013 4:15 pm
by danspaes
Hello all,

I didn't understood why does the 4th method wasn't called if bool1 is declared "true". The method aren't supposed to be called in case that bool1 is declared "false", as the comparison was done with a short term operator isn't that right?

Many thanks !

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

Posted: Sun Dec 15, 2013 11:54 pm
by admin
danspaes wrote:Hello all,

I didn't understood why does the 4th method wasn't called if bool1 is declared "true". The method aren't supposed to be called in case that bool1 is declared "false", as the comparison was done with a short term operator isn't that right?

Many thanks !
No, since bool1 is true, there is no need to compute the right hand side operand of || because the whole expression will be true irrespective of what the right hand side operand returns. You might want to go through this to understand this: http://stackoverflow.com/questions/8759 ... circuiting

HTH,
Paul.

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

Posted: Sun Jun 14, 2015 4:44 pm
by berensn
in line 2 it says: bool = ( bool2 && method1(i++) ); //2 where bool2= false, therefore it can't short circuit and should increment i making the answer 3 instead of 2.

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

Posted: Sun Jun 14, 2015 7:58 pm
by admin
berensn wrote:in line 2 it says: bool = ( bool2 && method1(i++) ); //2 where bool2= false, therefore it can't short circuit and should increment i making the answer 3 instead of 2.
Since bool2 is false, && will short circuit the expression. Why do you think it will not?
-Paul.

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

Posted: Mon Feb 20, 2023 4:04 am
by tonyincognito
berensn wrote:
Sun Jun 14, 2015 4:44 pm
in line 2 it says: bool = ( bool2 && method1(i++) ); //2 where bool2= false, therefore it can't short circuit and should increment i making the answer 3 instead of 2.
It's because && requires both operands to be true.

Because bool2 is false, it no longer checks the second operand because the statement will never be true (because bool2 was already false) and therefore it will not execute i++.

Hope that explains it better.