Why does the snippet below print true on second pass through? Should it not be a new instance?
import java.util.function.Supplier;
public class Foo {
public static void main(String[] args) throws Exception {
Supplier<Long> old = () -> System.nanoTime();
for (int i = 0; i < 3; i++) {
/* false true true
Supplier<Long> foo = System::nanoTime;*/
Supplier<Long> foo = () -> System.nanoTime();
/* false false false
Supplier<Long> foo = new Supplier<Long>() {
@Override
public Long get() {
return System.nanoTime();
}
};
//*/
System.out.printf("%s %s %s%n", foo == old, foo, old);
old = foo;
}
}
}
false Foo$$Lambda$2/122883338@1ddc4ec2 Foo$$Lambda$1/1534030866@133314b
true Foo$$Lambda$2/122883338@1ddc4ec2 Foo$$Lambda$2/122883338@1ddc4ec2
true Foo$$Lambda$2/122883338@1ddc4ec2 Foo$$Lambda$2/122883338@1ddc4ec2
A lambda in Java essentially consists of three parts: a parenthesized set of parameters, an arrow, and then a body, which can either be a single expression or a block of Java code.
Fortunately, you can assign lambda expressions to variables and reuse them, as you would with objects.
Oracle claims that use of lambda expressions also improve the collection libraries making it easier to iterate through, filter, and extract data from a collection. In addition, new concurrency features improve performance in multicore environments.
Check out this article about how lambdas are implemented.
Essentially the compiler turned your two System.nanoTime()
into the following static methods on your class:
static Long lambda$1() {
return System.nanoTime();
}
static Long lambda$2() {
return System.nanoTime();
}
And then created a constant reference to each target-typed to Supplier<Long>
using the LambdaMetaFactory
. Frankly, I'm disappointed that the Java compiler didn't realize that the lambda body was identical and only create one instance. If the Java compiler were sufficiently smart, every line should have printed true
!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With