Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding Functional Interface Methods and Method References  RSS feed

 
Jack Kenshin
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to go with definitions and their examples.



Its usage is

  

Here, my comprehension is `now` is factory method and referred to by `s1` and `s2`. Their types are `Supplier<LocalDate>`. I get from the type *`LocalDate::now`* returns `LocalDate` type.

Its usage,


`println` is the method taking String, returning nothing.

Lastly,

Their usages,



My problem is here that `Predicate` has a method declaration returning `boolean` and passing nothing. Doesn't it? In the above two examples at left-hand side among diamond operators there are types either return type or passing type. So, `isEmpty` declaration has no type for passing, `boolean` for returning. Same act applies for `BiPredicate` that `startsWith` takes just only one parameter type of `String`, returns boolean. How does it work? Because I think it should work with `Predicate` not with `BiPredicate`. But, in this case `isEmpty` shouldn't work with `Predicate` but works. I'm a little confused. Would you explain it?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37051
507
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Predicate takes a single parameter and returns a boolean. Clearly, isEmpty() returns a boolean so that part is good.

Where this gets tricky is that a method reference (or lambda) can supply EITHER the parameter or the object to be manipulated. In the case of isEmpty() the String is supplied as an instance of which isEmpty() is called on.
 
Jack Kenshin
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Predicate takes a single parameter and returns a boolean. Clearly, isEmpty() returns a boolean so that part is good.

Where this gets tricky is that a method reference (or lambda) can supply EITHER the parameter or the object to be manipulated. In the case of isEmpty() the String is supplied as an instance of which isEmpty() is called on.


Thank you indeed. The question is taken from that book which author is you.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!