LambdaCalculator.java - Lambda Expression Example

This section provides a tutorial example on using an lambda expression to define a class anonymously implementing a single abstract method interface. An anonymous class and a local class are used as comparisons.

My first lambda expression program, LambdaCalculator.java, follows an example given in a Oracle Java tutorial. Here is the source code:

```/* LambdaCalculator.java
- Copyright (c) 2014, HerongYang.com, All Rights Reserved.
*/
class LambdaCalculator {
interface Operation {
int operate(int a, int b);
}
public int calculate(int a, int b, Operation o) {
System.out.println(o.toString());
return o.operate(a, b);
}

public static void main(String[] arg) {
int x = 1;
String s = "+";
int y = 2;
if (arg.length>0) x = Integer.parseInt(arg[0]);
if (arg.length>1) s = arg[1];
if (arg.length>2) y = Integer.parseInt(arg[2]);

// Using a lambda expression
Operation add = (int a, int b) -> {return a+b;};

// Using anonymous class
Operation subtract = new Operation() {
public int operate(int a, int b) {return a-b;}
};

// Using a local class
class Multiply implements Operation {
public int operate(int a, int b) {return a*b;}
}
Operation multiply = new Multiply();

LambdaCalculator c = new LambdaCalculator();
if (s.equals("+"))
System.out.println(x+s+y+" = "+c.calculate(x,y,add));
else if (s.equals("-"))
System.out.println(x+s+y+" = "+c.calculate(x,y,subtract));
else if (s.equals("*"))
System.out.println(x+s+y+" = "+c.calculate(x,y,multiply));
else
System.out.println(x+s+y+" = Undefined");
}
}
```

Note that my sample program uses an lambda expression, an anonymous class and a local class to implement the same interface:

• "Operation" is a single abstract method interface which is nested in the "LambdaCalculator" class.
• "Operation add = (int a, int b) -> {return a+b;};" uses a lambda expression to implement an add operation. A new object is created from the implementation class and assigned to "add".
• "Operation subtract = new Operation() { ... };" uses an anonymous class to implement a subtract operation. A new object is created from the implementation class and assigned to "subtract".
• "Operation multiply = new Multiply();" uses a local class to implement a multiply operation. A new object is created from the implementation class and assigned to "multiply".

Now compile and run LambdaCalculator.java with JDK 1.8, you will get:

```C:\herong>java LambdaCalculator
LambdaCalculator\$\$Lambda\$1/518248@cf9800
1+2 = 3

C:\herong>java LambdaCalculator 8 + 2
LambdaCalculator\$\$Lambda\$1/518248@cf9800
8+2 = 10

C:\herong>java LambdaCalculator 8 - 2
LambdaCalculator\$1@cf9800
8-2 = 6

C:\herong>java LambdaCalculator 8 "*" 2
LambdaCalculator\$1Multiply@cf9800
8*2 = 16
```

Here is what I learned from the output:

• The lambda expression "(a,b) -> {return a+b;}" does define a class named as "LambdaCalculator\$\$Lambda\$1/518248".
• When using * as command line argument, it needs to be quoted as "*", because * is wildcard character on Windows systems.

If we look at class files generated by the compiler, we will see only 4 of them. Any there is no class file for the "LambdaCalculator\$\$Lambda\$1/518248" class defined by the lambda expression.

```C:\herong>dir LambdaCalculator*.*

1,423 LambdaCalculator.java
2,076 LambdaCalculator.class           - Main application class
214 LambdaCalculator\$Operation.class - Nested interface "Operation"
440 LambdaCalculator\$1.class         - Anonymous class for "subtract
459 LambdaCalculator\$1Multiply.class - Local class "Multiply"
```

Last update: 2014.

Table of Contents