Page 1 of 1

About Question enthuware.ocpjp.i.v11.2.3067 :

Posted: Tue Dec 10, 2019 1:06 pm
by demetrio
Question: Which of the following lambda expressions can be used to implement a Function<Integer, String> ?

WRONG OPTION) a -> Integer::toHexString

Integer::toHexString is a valid method reference but is not suitable as a body of a lambda expression because it is not possible to determine the argument that is to be passed to this method from the context. The given lambda expression will be converted into an anonymous class like this:

new Function(){    public String apply(Integer a){        return Integer::toHexString;  <- The method reference cannot be coverted into an expression that returns a String at this point.    }

---

CORRECT OPTION) Integer::toHexString

This method reference can be coverted to a functional interface implementation because there is only one possible value that can be passed as an argument to toHexString, which is the argument passed to the Funtion's apply method. Something like this:

new Function(){    public String apply(Integer arg){        return Integer.toHexString(arg);    } }


Observe the difference between how a method reference and a lambda expression are handled. When you use a method reference, it is "converted" into a method body. But when you use a lambda expression, the expression given in the lamba body is used as it is in the method body.

My issue:

I can´t figure out why return Integer::toHexString; is in first case and return Integer.toHexString(arg);. I am trying to find a concept or theory I could apply in exam if similar question pops up. Additionally, I understand that both case are method reference converted to method body since I see "::" (double colon). If so, what isn't "a -> Integer::toHexString" also converted to Integer.toHexString(a)?

Re: About Question enthuware.ocpjp.i.v11.2.3067 :

Posted: Wed Dec 11, 2019 12:23 am
by admin
The first case ( a -> Integer::toHexString ) is a lambda expression (which uses a method reference), while the second case ( Integer::toHexString ) is not a lambda expression. It is only a method reference. Both are converted into an inner classes but in different ways. This is what the explanation to the 3rd options highlights:
Observe the difference between how a method reference and a lambda expression are handled. When you use a method reference, it is "converted" into a method body. But when you use a lambda expression, the expression given in the lamba body is used as it is in the method body.
That is why a -> Integer::toHexString doesn't work but Integer::toHexString works.

(BTW, the problem statement is imprecise and should be fixed. It should say, "Which of the following expressions...", instead of "Which of the following lambda expressions...")

Re: About Question enthuware.ocpjp.i.v11.2.3067 :

Posted: Mon Feb 17, 2020 12:35 pm
by Dani1515
hi, I am preparing for the exam with the book "Java SE 11 Programmer 1 Exam Fundamentals 1Z0-815" from Mr. H. deshmukh, this Functional Interface is not contained in the chapter about lambda expressions, my question is: Can I nevertheless expect a question about that f. I. on the exam or not?
In the mock exams are also questions about Wrapper Classes, but they are not anymore on the OCP Java 11 Part 1 exam (source: the mentioned book), so I am little bit confused.

Re: About Question enthuware.ocpjp.i.v11.2.3067 :

Posted: Mon Feb 17, 2020 9:47 pm
by admin
Hi,
The java.util.function.Function is not officially on the exam but we have unconfirmed reports that some people got a question that referred to this interface. That is why it is not in the book but it is there in the mock exams (only a couple of questions). Our suggestion is to just go through it once if you have time. We may add it in the book later after it is confirmed from multiple sources.

Wrapper classes are are covered in the book (in detail) as well as in the mock exams. There is no confusion on this topic. You have to study it.

Please note that mock exams are more dynamic than the book and so you may find a few questions in the mock exams that are newly added (due to feedback received from users) but not in the book. Eventually, such things will be added in the book also.

HTH,
Paul.

Re: About Question enthuware.ocpjp.i.v11.2.3067 :

Posted: Tue Jun 09, 2020 12:01 am
by dimitrilc
Yeah the double colon operator "::" was not mentioned anywhere in the book. I got this question wrong, too.

Re: About Question enthuware.ocpjp.i.v11.2.3067 :

Posted: Tue Jun 09, 2020 2:41 am
by admin
As explained above:
real exam < book < mock exams.

Book will cover some more than the real exam. Mock exams will cover a bit more than the book.
Also see the motivation here: viewtopic.php?f=2&t=5562

Re: About Question enthuware.ocpjp.i.v11.2.3067 :

Posted: Sun Sep 15, 2024 1:18 am
by _umut_
Hi,

"i::toHexString
This invalid because i is unknown and it is not a valid way to get a method reference."

I found the explanation for i::toHexString a bit misleading. The issue is that toHexString is a static method, so it should be referenced using the class name (Integer::toHexString), not an instance. Additionally, there’s no instance method in java.lang.Integer that matches Function<Integer, String>, which makes i::toHexString invalid for this purpose (and of course there is no i defined in this example).

A clarification on this would be helpful.

Re: About Question enthuware.ocpjp.i.v11.2.3067 :

Posted: Sun Sep 15, 2024 7:51 pm
by admin
Sure. Will update the explanation to make this point clear.
Thank you for your feedback!