I noticed new objects are not being created in case of lambdas unlike anonymous classes. Could someone help me understand this please?
//case 1: anonymous class
for (int i = 0; i < 3; i++) {
Runnable r = new Runnable() { @Override public void run() { System.out.println("blah");}};
System.out.println("Anonymous: " + r.hashCode());
}
//case 2: lambdas
for (int i = 0; i < 3; i++) {
Runnable r = () -> System.out.println("blah");
System.out.println("Lambda: " + r.hashCode());
}
Prints
Lambda: 1915503092
Lambda: 1915503092
Lambda: 1915503092
Anonymous: 1535128843
Anonymous: 1567581361
Anonymous: 849460928