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 a new one:
System.out.println(x1());
System.out.println(x1());
System.out.println(x2());
System.out.println(x2());
It 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 the JVM specification I suppose) is this rule of the lambda reuse described? How does JVM decide when to reuse or not?
You can't be sure about the identity of the object returned for a lambda expression. It can be a new instance, or a pre-existing instance.
This is specified in JLS §15.27.4:
At run time, evaluation of a lambda expression is similar to evaluation of a class instance creation expression, insofar as normal completion produces a reference to an object. Evaluation of a lambda expression is distinct from execution of the lambda body.
Either a new instance of a class with the properties below is allocated and initialized, or an existing instance of a class with the properties below is referenced. If a new instance is to be created, but there is insufficient space to allocate the object, evaluation of the lambda expression completes abruptly by throwing an OutOfMemoryError.