Page 1 of 1

About Question enthuware.ocpjp.v8.2.1873 :

Posted: Sun Sep 27, 2020 3:06 pm
by Javier
Hi Paul!
I have a question about one book that you recommended me: "Java 8 in Action" (Raoul-Gabriel Urma, Mario Fusco, Alan Mycroft).
In the page 83, at the beginning of the page says:
Table 3.4. Examples of lambdas and method reference equivalents

Code: Select all

() -> Thread.currentThread().dumpStack()

Code: Select all

Thread.currentThread()::dumpStack
Paul, I am not able to assignate this last method reference to a Functional Interface, I think should be a Supplier interface because doesn´t have any parameters, but the compiler is saying that dumpStack is void, so Supplier doesn´t work here... and I don´t know if the type of the interface should be Thread. I did something like this:
Supplier<Thread> function = Thread.currentThread()::dumpStack; // not compile
Could you help me to assignate correctly this method reference please?
Thank you in advance

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

Posted: Sun Sep 27, 2020 10:35 pm
by admin
The functional method of Supplier doesn't have any parameters but it has a return type. Every lambda expression or method reference is converted into an inner class by the compiler (Go through this: https://enthuware.com/lambda-for-ocajp ). How do you think the method reference Thread.currentThread()::dumpStack; can be used in that inner class to return a value?

Thread.currentThread()::dumpStack; can be use to implement a functional interface with the functional method that doesn't take any parameter and doesn't return a value. Is there any such standard functional interface? If no, you can define one and try it out.

Also, I am not sure in what context or what purpose I recommended "Java 8 in Action" book. But you might also want to get help from the author of that book.
Paul.