About Question enthuware.ocajp.i.v8.2.1464 :
Moderator: admin
-
- Posts: 2
- Joined: Thu Jan 07, 2016 12:55 am
- Contact:
About Question enthuware.ocajp.i.v8.2.1464 :
This question is confusing and couldn't convey the objective effectively due to typos. e.g I don't understand how to interpret the statement return k-->0?true:false;
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1464 :
Can you please tell me what typos did you see so that I can confirm?
Did you read the detailed explanation? It explains exactly how to to interpret the statement that you mentioned. Please let me know which part do you have trouble with so that I can elaborate it further.
thank you,
Paul.
Did you read the detailed explanation? It explains exactly how to to interpret the statement that you mentioned. Please let me know which part do you have trouble with so that I can elaborate it further.
thank you,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Thu Jan 07, 2016 12:55 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1464 :
Hi Paul,
Sorry for the delayed response but the statement I quoted "return k-->0?true:false;" has typo ("-->") is not a valid operator ... wait a sec, maybe I am wrong; now I get it; I think it should be read as k-- > 0 ? true : false ;
Looks like k-- is post decrement.
Sorry for the delayed response but the statement I quoted "return k-->0?true:false;" has typo ("-->") is not a valid operator ... wait a sec, maybe I am wrong; now I get it; I think it should be read as k-- > 0 ? true : false ;
Looks like k-- is post decrement.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1464 :
That is correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Thu Jan 26, 2017 8:45 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1464 :
Answer and explanation give: "Replace while(checkIt(k)) with while(checkIt(--k)). It will print 4321." And further: "Observe that the method parameter k in checkIt shadows the instance variable k. Therefore, any changes made to k in checkIt will not affect the instance variable k. For checkIt method to access the instance variable k, you need to do this.k."
Why does diminishing the shadowed variable k in checkIt still output 4321? Why is this not printing 5 5 5 5 5?
Why does diminishing the shadowed variable k in checkIt still output 4321? Why is this not printing 5 5 5 5 5?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1464 :
Because the instance variable k is first being decremented due to --k part of the call to checkIt(--k). The decremented value is being passed to checkIt method. Now, the method checkIt also decrements k but this k refers to the method parameter and has no impact on the instance variable k. So the instance variable k is not decremented twice but only once per call to checkIt(--k).PeterD wrote:Answer and explanation give: "Replace while(checkIt(k)) with while(checkIt(--k)). It will print 4321." And further: "Observe that the method parameter k in checkIt shadows the instance variable k. Therefore, any changes made to k in checkIt will not affect the instance variable k. For checkIt method to access the instance variable k, you need to do this.k."
Why does diminishing the shadowed variable k in checkIt still output 4321? Why is this not printing 5 5 5 5 5?
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Wed Jul 05, 2017 3:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1464 :
I would like to understand why the wrong to replace to return this.k-->0?true:false resolves to 43210. Could you provide an explanation? Where does the instance variable get decremented? When I execute the code and I put a System.out.println(this.k) in the checkIt method before the lambda executes I see 5, 44, 33, 22, 11, 00.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1464 :
This instance variable k gets decremented because of "this.k--" contained in the statement, return this.k-->0?true:false;
First, k's value is compared with 0. Based on the comparison, the return value (true or false) is determined. In the first iteration, k is 5, so return value is decided as true.
Next, k's value is decremented. So k becomes 4.
Now, the value that was determined in 1st step is returned by checkIt method i.e. true.
First, k's value is compared with 0. Based on the comparison, the return value (true or false) is determined. In the first iteration, k is 5, so return value is decided as true.
Next, k's value is decremented. So k becomes 4.
Now, the value that was determined in 1st step is returned by checkIt method i.e. true.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 26 guests