About Question enthuware.ocpjp.i.v11.2.3067 :
Posted: Tue Dec 10, 2019 1:06 pm
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)?
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)?