Created
May 30, 2015 12:02
-
-
Save orekyuu/ccae69dcc1d903513845 to your computer and use it in GitHub Desktop.
匿名クラスとラムダと時々弱参照
This file contains hidden or 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
import java.lang.ref.WeakReference; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
Main main = new Main(); | |
main.run(); | |
} | |
private void run() { | |
Hoge hoge1 = new Hoge() { | |
@Override | |
public void run() { | |
System.out.println("匿名クラス"); | |
} | |
}; | |
Hoge hoge2 = () -> System.out.println("ラムダ"); | |
WeakReference<Hoge> a = new WeakReference<>(hoge1); | |
WeakReference<Hoge> b = new WeakReference<>(hoge2); | |
hoge1 = null; | |
hoge2 = null; | |
System.gc(); | |
System.out.println(a.get()); | |
System.out.println(b.get()); | |
} | |
} | |
@FunctionalInterface | |
interface Hoge { | |
void run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
出力
null
Main$$Lambda$1/295530567@3b07d329