HI Enthware,
We have remembered when we used curly braces we have to use return statement in lambda expression.
But here we don't use it.
dList.stream().forEach(x->{ x = x+10; });
Please help me.
About Question enthuware.ocpjp.v8.2.1819 :
Moderator: admin
-
- Posts: 4
- Joined: Wed Jun 05, 2019 6:44 am
- Contact:
-
- Posts: 4
- Joined: Wed Jun 05, 2019 6:44 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1819 :
Here is screenshot from from OCA book ie OCA Oracle Certified Associate Java SE 8 Programmer 1 Study Guide" by Jeanne Boyarsky and Scott Selikoff
- Attachments
-
- enthware.jpg (30.8 KiB) Viewed 2777 times
-
- Site Admin
- Posts: 10385
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1819 :
Can't comment on what the book says, you should contact the author for that. But a return statement is required with {} if the functional method needs to return a value.
-
- Posts: 33
- Joined: Sat Feb 15, 2020 12:44 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1819 :
I also was confused by the missing return type and that's how I did not grasp the more interesting concept of passing by values if the object is immutable. I am writing it to remember better.
The return type is not necessary, because forEach() uses a consumer and a consumer returns void. It would probably also be a compiler error to add a return statement. I am such a Pro!

Who is online
Users browsing this forum: Google [Bot] and 12 guests