The first two cases yield false
because add1Test == and1Test
(as well as foo == foo
) compares two different objects which implement Function1
interface. add1Test == and1Test
translates to something like new AbstractFunction1() { ... }.equals(new AbstractFunction1() { ... })
(due to the specialization of primitives the actual code is a bit more complicated but the idea is the same). Since the left side and the right side refer to different objects and the equivalence of functions is just equivalence of their instances, the result is false
.
The third case compares the same object (because it was created via val
) and therefore returns true
.