About Question enthuware.ocpjp.v8.2.1900 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
jorgeruiz
Posts: 4
Joined: Sat Jan 20, 2018 1:36 pm
Contact:

About Question enthuware.ocpjp.v8.2.1900 :

Post by jorgeruiz »

The first option is in fact a wrong answer. And it performs as in the explanation.

However,
Why the lambda in the first call to forEach() is not treated as a Function/UnuaryOperator instead of a Consumer since the String.toUpperCase() returns a String?

Code: Select all

letters.forEach(letter->letter.toUpperCase());

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

Re: About Question enthuware.ocpjp.v8.2.1900 :

Post by admin »

Because the forEach method is defined to expect a Consumer as an argument and not a Function.
If you like our products and services, please help us by posting your review here.

jorgeruiz
Posts: 4
Joined: Sat Jan 20, 2018 1:36 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1900 :

Post by jorgeruiz »

Yes, but that should not cause the code to not compile since the lambda is returning a value meanwhile the return type of Consumer is void?

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

Re: About Question enthuware.ocpjp.v8.2.1900 :

Post by admin »

Sorry, not sure what you mean. What should not cause what to not compile? A method that returns void can call a method that returns anything within its body -
void m(){
methodThatReturnsSomething(); //no problem here. return value is ignored
}

But if a method expects a Consumer as argument, you can't pass it a Function (and vice versa).
So I am not sure what is the confusion.
If you like our products and services, please help us by posting your review here.

jorgeruiz
Posts: 4
Joined: Sat Jan 20, 2018 1:36 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1900 :

Post by jorgeruiz »

Ok. I got you.
Since forEach expects a Consumer. What is happening here is:

Code: Select all

(String s) -> { "somestring".toUpperCase(); }
What I was thinking is, if the toUpperCase() method returns a value, then the lambda should be treated as a Function:

Code: Select all

 (String s) -> { RETURN "somestring".toUpperCase(); }
I'm sorry my question got confused.
Thanks.

gfinesch
Posts: 2
Joined: Thu Nov 01, 2018 8:29 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1900 :

Post by gfinesch »

The first statement does convert each element to upper case. However, the new upper case value does not get back in the the list. It is lost. Therefore, the second statement still prints the lower case value.
Does the upper case value get lost because of String's immutability, right?

In fact, were it be like

Code: Select all

 List<StringBuilder> letters = Arrays.asList(new StringBuilder ("j"), new StringBuilder("a"), new StringBuilder("v"),new StringBuilder("a")); 
 letters.forEach(letter->letter.append("BYE")); 
letters.forEach(System.out::print);
The output would have been

Code: Select all

jBYEaBYEvBYEaBYE
Thanks!

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

Re: About Question enthuware.ocpjp.v8.2.1900 :

Post by admin »

That's right. But you should try it out :)
If you like our products and services, please help us by posting your review here.

gfinesch
Posts: 2
Joined: Thu Nov 01, 2018 8:29 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1900 :

Post by gfinesch »

Thank you. Yes, I tied it out. I just wanted to be 100% sure why it works that way.

Post Reply

Who is online

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