Java Tutorials - Herong's Tutorial Examples - Version 7.03, by Dr. Herong Yang
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:
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:
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
Execution Process, Entry Point, Input and Output
Primitive Data Types and Literals
Bits, Bytes, Bitwise and Shift Operations
Managing Bit Strings in Byte Arrays
Reference Data Types and Variables
StringBuffer - The String Buffer Class
System Properties and Runtime Object Methods
Generic Classes and Parameterized Types
Generic Methods and Type Inference
►Lambda Expressions and Method References
►LambdaCalculator.java - Lambda Expression Example
Lambda Expression Syntax Options
Lambda Expression as Method Reference
Method Reference Example - LambdaMethodRefernce.java
Lambda Expression Stream Pipeline Operations
Execution Threads and Multi-Threading Java Programs
ThreadGroup Class and "system" ThreadGroup Tree
Synchronization Technique and Synchronized Code Blocks
Deadlock Condition Example Programs