Primitive specialisations and the cost of boxing
Every boxed result outside -128..127 is a new Integer object. The Int* interfaces never create one.
A functional interface is an interface with exactly one abstract method, and it is the only kind of type a lambda can become. java.util.function provides the standard shapes (Supplier, Consumer, Function, Predicate and friends), plus primitive versions that avoid boxing.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Build a Function<String, String> called clean by composing String::trim and String::toLowerCase with andThen, then apply it to " HeLLo World " and print the result in square brackets.
Write a method static List<Integer> filter(int[] nums, IntPredicate rule) and use it with a predicate for "even and greater than 10" built from two IntPredicates and and. Use {4, 12, 15, 20, 7}.
Declare @FunctionalInterface interface TriFunction<A, B, C, R> { R apply(A a, B b, C c); } and use it to compute a price from quantity, unit price and a discount percentage: 3, 250.0, 10 should give 675.0.
Explain it without notes
What exactly qualifies an interface as a functional interface?
Name the four main families in java.util.function and their method names.
Why do primitive specialisations like IntPredicate exist?
How do you deal with checked exceptions inside lambdas?
Expected output
sum of odd squares 1..5 = 35
length of 'stream' = 6
10000 boxed twice: equals true, same object false
100 boxed twice: same object true