I'm kinda starting with Java 8 and I'm wondering about some best practices. I really like how i can write inline lambdas but how does this work in the background ?
I have two cases:
Using a lambda that requires an in-scope value:
private int x;
void doIt(Stream<Integer> stream) {
stream.map(i -> i * x).forEach(...);
}
Using a lambda that is whole on it's own:
void doIt(Stream<Integer> stream) {
stream.map(i -> i * i).forEach(...);
}
In the latest case I could actually declare the lambda as a constant:
private static final Function<Integer, Integer> squaring = i -> i * i;
void doIt(Stream<Integer> stream) {
stream.map(squaring).forEach(...);
}
Is there any impact on the performance or is the compiler smart enough to do what's best depending on the case ?