Skip to content

Instantly share code, notes, and snippets.

@YukiMatsumura
Created April 1, 2015 13:32
Show Gist options
  • Save YukiMatsumura/372ee16420a3a61abcd0 to your computer and use it in GitHub Desktop.
Save YukiMatsumura/372ee16420a3a61abcd0 to your computer and use it in GitHub Desktop.
// Java7 non-Lambda
Runnable r = new Runnable() {
public void run() {
System.out.println("Howdy, world!");
}
};
r.run();
// Java8 Lambda
Runnable r2 = () -> System.out.println("Howdy, world!");
r2.run();
@FunctionalInterface
interface Hoge {
public String foo();
}
public class Main {
@FunctionalInterface
interface Lambda {
public void print();
}
public static void main(String... args) {
new Main().execute();
}
private void execute() {
new Hoge().foo(() -> System.out.println(this));
}
}
class Hoge {
public void foo(Main.Lambda lambda) {
lambda.print();
}
}
// 出力結果: Main@5ca881b5
// lambda.print()で得られるthisはHogeではなくMainを指す.
// 出力結果: Main$1@15db9742
public class Main {
public static void main(String... args) {
new Main().execute();
}
public void execute() {
Runnable r = new Runnable() {
public void run() {
System.out.println(this);
}
};
r.run();
}
}
// 出力結果: Main@15db9742
public class Main {
public static void main(String... args) {
new Main().execute();
}
public void execute() {
Runnable r = ()-> System.out.println(this);
r.run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment