Java Tutorials - Herong's Tutorial Examples - Version 7.03, by Dr. Herong Yang
Lambda Expression Syntax Options
This section describes some syntax simplification options on lambda expressions. A sample program is provided to show you a very compact example of a lambda expression: (a->b->c->d->'Hello').
Java 8 also supports several options to simply the lambda expression syntax:
Using the simplified syntax makes the source compact and easier to read in most cases. But it do the opposite. For example, take a guess on what this lambda express will do in your Java code: (a->b->c->d->"Hello")?
You can play the following sample program to find out:
/* LambdaNested.java - Copyright (c) 2014, HerongYang.com, All Rights Reserved. */ class LambdaNested { static void show(InterfaceA a) { System.out.println(a.toString()); } public static void main(String[] arg) { show(a->b->c->d->"Hello"); } interface InterfaceD { Object methodD(Object o); } interface InterfaceC { InterfaceD methodC(Object o); } interface InterfaceB { InterfaceC methodB(Object o); } interface InterfaceA { InterfaceB methodA(Object o); } }
This program will pass the compilation and run in JDK 1.8:
C:\herong>javac LambdaNested.java C:\herong>java LambdaNested LambdaNested$$Lambda$1/518248@cf9800
If you are having trouble reading the above program, the following version using anonymous classes may help you out:
/* LambdaNestedDecoded.java - Copyright (c) 2014, HerongYang.com, All Rights Reserved. */ class LambdaNestedDecoded { static void show(InterfaceA a) { System.out.println(a.toString()); } public static void main(String[] arg) { // show(a->b->c->d->"Hello"); // InterfaceD iD = d->"Hello"; InterfaceD iD = new InterfaceD() { public Object methodD(Object o) {return "Hello";} }; // InterfaceC iC = c->iD; InterfaceC iC = new InterfaceC() { public InterfaceD methodC(Object o) {return iD;} }; // InterfaceB iB = b->iC; InterfaceB iB = new InterfaceB() { public InterfaceC methodB(Object o) {return iC;} }; // InterfaceA iA = a->iB; InterfaceA iA = new InterfaceA() { public InterfaceB methodA(Object o) {return iB;} }; show(iA); } interface InterfaceD { Object methodD(Object o); } interface InterfaceC { InterfaceD methodC(Object o); } interface InterfaceB { InterfaceC methodB(Object o); } interface InterfaceA { InterfaceB methodA(Object o); } }
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