• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

!!!!! Stream.filter() is not showing compile time error even with an invalid predicate

 
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the below code I am using a filter and a filter accepts a Predicate interface as parameter. We all know that Predicate has one method named test which accepts one parameter and returns a boolean.
I am of the understanding that the isEven() method that accepts no parameter is not a valid Predicate because unlike test() method it does not take any parameter, so how come my code is not showing a compile time error?




Also, would someone be kind enough to provide me a good source for understanding method reference, please.
 
author & internet detective
Posts: 39433
768
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know why it doesn't work as one would expect, but it's interesting. I flagged this post to get it some more attention.
 
Saloon Keeper
Posts: 3443
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a wild guess. Does a parameterless method in a class have an implicit paramer: Class this?

For instance:

It works for me.

If that is so, then that would explain why the method reference works. Do I remember correctly that Stephan wrote one or more times about a parameter involving this?
 
Saloon Keeper
Posts: 10495
224
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The syntax that Piet showed is called "receiver parameter" and its only purpose is to allow the programmer to annotate the this keyword. It's very useful as a visual aid in explaining how method handles can be used though.

Yes, you can think of instance methods as having an implicit parameter for the object you're calling the method on. That's what happens under the hood anyway.

A parameterless instance method can either be used as a function that maps an instance of the declaring class to the return type of the method, or as a nullary function that just returns the return type of the method. In the latter case you'd use the method handle operator on an expression and not on a type name.
 
Marshal
Posts: 65366
248
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Stephan means is that the compiler will interpret Data::isEven as the same as d -> d.isEven()
And we know the latter is a valid λ for a Predicate::test() call. Using the class name in the method reference makes it look like a static method call, but it isn't static.
 
shahid sarwar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I believe due to the implicit "this" a call to is the same as  
 
Campbell Ritchie
Marshal
Posts: 65366
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shahid sarwar wrote:So I believe due to the implicit "this" a call to is the same as  

No, it isn't. Both might give the same answer but semantically they are different.

The syntax of that method reference might look like a call to a static method, but it actually shorthand for calling an instance method. It is possible to write a static method whose method reference looks the same. The fact that two method references look the same does not imply that the two methods are the same. Only that they have the same name.
 
We don't have time for this. We've gotta save the moon! Or check this out:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!