About Question enthuware.ocpjp.v8.2.1873 :

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

Moderator: admin

Post Reply
Posts: 62
Joined: Mon Feb 20, 2017 12:31 pm

About Question enthuware.ocpjp.v8.2.1873 :

Post 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

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

Site Admin
Posts: 8943
Joined: Fri Sep 10, 2010 9:26 pm

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests