Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Doubt - Predicate  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain this code ? Specifically why pre3 doesn't compile but pre2 does.

 
Rancher
Posts: 427
6
Fedora IntelliJ IDE Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Predicate<String>  accepts a String and returns a boolean.  How can you call a method of another object that is unrelated to the input object.  The pre2 compiles i'm guessing because it will interpret it as


If pre3 worked then it would look like this:

Doesn't make any sense does it?  that's why it won't compile.

If it was another object that had a method that accepts a String and returns a boolean then it will compile.
 
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 64471
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are doing in a Predicate<T> is taking a T an applying it to the test() method. But test() takes a T as its parameter. So you have a T somewhere.
You are trying to find the equivalent of s.isEmpty(). The test(T) call must use the T somewhere, so it goes into the λ to work out where. It would usually execute s.isEmpty() in which case you write the method reference as String::isEmpty. That might look like a static method call, but it isn't because isEmpty() is an instance method. The T cannot be applied to the method because isEmpty() takes no parameters, so the no‑args method is called, and the T is transferred to become the object on which the method is called.
If you write str::isEmpty, the object name has already been used, so there is nowhere to pass an object anywhere.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!