Skip to content

Instantly share code, notes, and snippets.

@orekyuu
Created May 30, 2015 12:02
Show Gist options
  • Save orekyuu/ccae69dcc1d903513845 to your computer and use it in GitHub Desktop.
Save orekyuu/ccae69dcc1d903513845 to your computer and use it in GitHub Desktop.
匿名クラスとラムダと時々弱参照
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();
}
@orekyuu
Copy link
Author

orekyuu commented May 30, 2015

出力
null
Main$$Lambda$1/295530567@3b07d329

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment