About Question enthuware.ocpjp.v8.2.1885 :

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

Moderator: admin

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

About Question enthuware.ocpjp.v8.2.1885 :

Post by Javier »

Hi Paul!
I don´t understand why in some cases the Consumer function ignores the return type returned from the function and in other cases not.

Code: Select all

Consumer<Book> c = b->b.getId()+":"+b.getTitle();// this doesn´t compile, the type of the expression must be void, here is a String.

Code: Select all

Consumer<Book> c = b->b.getTitle();// but here compiles, the type of the expression is String as the previous one.
Paul could you explain me why is this behaviour, in one is ignoring the return type and in the other one not???
Thank you in advance

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

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

Post by admin »

b.getId()+":"+b.getTitle(); is an expression but not a valid statement, while b.getTitle(); is an expression as well as a valid statement.
It is like you can't just have, 2 + 3; in a method

{
...
2+3; //will not compile.
Math.random();//will compile.
...
}

This is explained in detail in Section 6.1.2 Expressions and Statements of Hanumant Deshmukh's OCP Java 11 Fundamentals book.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 62
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post by Javier »

Thank you Paul

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests