In Java, the lambda syntax is, simply, a way to describe the behaviour of a method without giving it a name.
e.g. (Type1 arg1, Type2 arg2, ...) -> <expression>
defines a function looking something like this:
public TypeOfExpression anonymousMethod(Type1 arg1, Type2 arg2, ...) {
...
return <expression>;
}
Look on the Java Tutorials for more info on lambdas.
In fact the lambda expression (boolean x, boolean y) -> x && y
(when given type MyClass
as you define it) is nothing but syntactic sugar for the following:
new MyClass() {
public boolean doSomething(boolean x, boolean y) {
return x && y;
}
}
Hopefully that gives some clarity into what lambdas are representing in Java.
In technical terms, the interface definition you gave defines a 'function type'. In Java, any interface which has only a single method is called 'functional', and can be used, instead of as an interface, as the type of a lambda expression of the same type as the single method in the interface.
This decision was presumably made because if an interface has only one method, all that is needed to specify it is the behaviour of that method, which can be far more concisely written in the lambda notation, as demonstrated above.
So, the lambda expression is nothing but an instance of your interface - in particular, it has no fields, but defines a method behaviour. I think you are assuming instead that it encapsulates a pair of inputs and their output result. If this is what you need, you might be looking for something more like a pair type with a built in operation (like &&
in this case).
EDIT: on serializability.
It's apparent that you require serialization of an unknown function of MyClass type. If MyClass
extends Serializable
, you should be able to cast appropriately and serialize. If you're not in control of MyClass
, so can't ensure this, the following ad hoc observation may help.
If the method in MyClass
is indeed of the type shown here (boolean, boolean) -> boolean
, there are a small enough number of cases to consider that you can manually serialize.
The number of 'possible' (total) functions of type A -> B
, for finite-sized types A
and B
is |B|
^|A|
(size of B
to the power size of A
). This is because, counting the possible functions, for each input of type A
there are |B|
possible outputs the function could give.
Also, the size of the pair type (A, B)
is |A|
*|B|
.
In our case, we have (boolean, boolean) -> boolean
. But boolean
is only of size 2! So (boolean, boolean)
is of size 4, and (boolean, boolean) -> boolean
is of size 2^4 = 16.
How do we work out which of these 16 functions we have on our hands? Simply enumerate through the possible inputs (only 4 to do). Then we can record the four outputs in variables something like this (where a
will be our unknown function.)
boolean ttout = a(true, true);
boolean tfout = a(true, false);
boolean ftout = a(false, true);
boolean ffout = a(false, false);
Then we can merrily serialize these four values. :)
Further, if we are deserializing and obtain the four values (ttout
, tfout
...) we can reconstruct the function something like this:
a = (boolean x, boolean y) -> {
if (x) {
if (y) return ttout;
else return tfout;
} else {
if (y) return ftout;
else return ffout;
}
}