Consider the following code fragment:
public static Object o = new Object();
public static Callable x1() {
Object x = o;
return () -> x;
}
public static Callable x2() {
return () -> o;
}
Method x2()
will always return the same lamba object, while x1()
will always create new one:
System.out.println(x1());
System.out.println(x1());
System.out.println(x2());
System.out.println(x2());
will printout something like this:
TestLambda$$Lambda$1/821270929@4a574795
TestLambda$$Lambda$1/821270929@f6f4d33
TestLambda$$Lambda$2/603742814@7adf9f5f
TestLambda$$Lambda$2/603742814@7adf9f5f
Where (in JVM specification I guess?) is this rule of lambda reuse described? How does JVM decide where do reuse or not?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…