lambda - Java 8: Difference between method reference Bound Receiver and UnBound Receiver -
i trying use java 8 method references in code. there 4 typed of method references available.
- static method reference.
- instance method (bound receiver).
- instance method (unbound receiver).
- constructor reference.
with static method reference
, constructor reference
have no problem, instance method (bound receiver)
, instance method (unbound receiver)
confused me. in bound
receiver use object reference variable calling method like:
objectref::instance method
in unbound
receiver use class name calling method like:
classname::instance method.
i have following question:
- what need different types of method references instance methods?
- what difference between
bound
,unbound
receiver method references? - where use
bound
receiver , useunbound
receiver?
i finding explanation of bound
, unbound
receiver java 8 language features books, still confused actual concept.
the idea of unbound receiver such string::length
you’re referring method object will supplied 1 of parameters of lambda. example, lambda expression (string s) -> s.touppercase()
can rewritten string::touppercase
.
but bounded refers situation when you’re calling method in lambda external object exists. example, lambda expression () -> expensivetransaction.getvalue()
can rewritten expensivetransaction::getvalue
.
situations 3 different ways of method reference
(args) -> classname.staticmethod(args)
can classname::staticmethod
(arg0, rest) -> arg0.instancemethod(rest)
can classname::instancemethod
(arg0
of type classname
)
(args) -> expr.instancemethod(args)
can expr::instancemethod
answer retired java 8 in action book
Comments
Post a Comment