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

Moderator: admin

Post Reply
The_Nick

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

Post by The_Nick » Sat Mar 09, 2013 8:33 am

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

admin
Site Admin
Posts: 8558
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sat Mar 09, 2013 8:41 am

Because increment is not being done in method1. It is done in the place where the method is called.
If you like our products and services, please help us by posting your review here.

danspaes
Posts: 1
Joined: Sun Dec 15, 2013 4:08 pm
Contact:

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

Post by danspaes » Sun Dec 15, 2013 4:15 pm

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 !

admin
Site Admin
Posts: 8558
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sun Dec 15, 2013 11:54 pm

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.
If you like our products and services, please help us by posting your review here.

berensn
Posts: 4
Joined: Sat Jun 13, 2015 3:08 pm
Contact:

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

Post by berensn » 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.

admin
Site Admin
Posts: 8558
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sun Jun 14, 2015 7:58 pm

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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 13 guests