About Question enthuware.ocajp.i.v7.2.1038 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1038 :
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
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
-
- Site Admin
- Posts: 10074
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1038 :
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.
-
- Posts: 1
- Joined: Sun Dec 15, 2013 4:08 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1038 :
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 !
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 !
-
- Site Admin
- Posts: 10074
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1038 :
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 ... circuitingdanspaes 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 !
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Sat Jun 13, 2015 3:08 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1038 :
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.
-
- Site Admin
- Posts: 10074
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1038 :
Since bool2 is false, && will short circuit the expression. Why do you think it will not?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.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Mon Feb 20, 2023 4:02 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1038 :
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.
Who is online
Users browsing this forum: No registered users and 18 guests