Q58: What are Method References and what are the four syntax variants?
Method References are shorthand lambda syntaxes (Class::method).
- Static Method:
ContainingClass::staticMethodName(Math::max) - Instance Method of Particular Object:
containingObject::instanceMethodName(System.out::println) - Instance Method of Arbitrary Object of Type:
ContainingType::methodName(String::toUpperCase) - Constructor Reference:
ClassName::new(ArrayList::new)
Key Takeaways
- Method references provide clean syntax shortcuts for lambda methods.
Knowledge Check
Which method reference syntax corresponds to the lambda expression (String s) -> s.toLowerCase()?