Skip to content

Instantly share code, notes, and snippets.

@iaveryanov
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iaveryanov/11140344 to your computer and use it in GitHub Desktop.
Save iaveryanov/11140344 to your computer and use it in GitHub Desktop.
Puzzle solution.
package ru;
// origin source: http://arhipov.blogspot.ru/2014/02/java-8-lambdas-unintentional-puzzle.html
// my gist with puzzle https://gist.github.com/iaveryanov/10990181
public class Forrest {
public Runnable wrooom(){
return new Runnable() {
@Override
public void run() {
System.out.println("Hello, lambda!");
}
};
}
public static void main(String[] args) {
Forrest forrest = new Forrest();
Runnable runnable = forrest.wrooom();
/**
* Method reference forrest::wrooom will expand to
*/
Runnable runnable2 = new Runnable() {
@Override
public void run() {
// not print
forrest.wrooom(); // return new Runnable, but will not be called run() with println!
// will print
// forrest.wrooom().run(); // return new Runnable and call run!
}
};
runnable.run(); // print "Hello, lambda!"
runnable2.run(); // print nothing, only create new Runnable!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment