Created
April 1, 2015 13:32
-
-
Save YukiMatsumura/372ee16420a3a61abcd0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@FunctionalInterface | |
interface Hoge { | |
public String foo(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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を指す. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 出力結果: 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