Four forms
public class Example {
public static void main(String[] args) {
Function<String, Integer> parse = Integer::parseInt; // static
Consumer<String> print = System.out::println; // bound instance
Function<String, String> upper = String::toUpperCase; // unbound instance
Supplier<ArrayList<String>> create = ArrayList::new; // constructor
}
}
Form 3 is the subtle one: String::toUpperCase becomes text -> text.toUpperCase() — the first parameter is the receiver.
The rest of this guide is part of Pro
Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.
Still to come · 1 section
- When a lambda is still better
Test yourself
Answer these before moving on — recall is what makes it stick.