Java 8 is the most awaited and is a major feature release of Java programming language. This is an introductory tutorial that explains the basic-to-advanced features of Java 8 and their usage in a simple and intuitive way.
Java 8 got released on March 18, 2014. There are several new features that are introduced in this release. I have covered all the Java 8 features in the separate guides. Here are the links to all the Java 8 tutorials in the systematic order.
This tutorial will be useful for most Java developers, starting from beginners to experts. After completing this tutorial, you will find yourself at a moderate level of expertise in Java 8, from where you can take yourself to next levels.
Knowledge of basic Java programming language is the only prerequisite for learning the concepts explained in this tutorial.
Lambda expressions are introduced in Java 8 and are touted to be the biggest feature of Java 8. Lambda expression facilitates functional programming, and simplifies the development a lot.
Syntax
A lambda expression is characterized by the following syntax.
Following are the important characteristics of a lambda expression.
- Optional type declaration − No need to declare the type of a parameter. The compiler can inference the same from the value of the parameter.
- Optional parenthesis around parameter − No need to declare a single parameter in parenthesis. For multiple parameters, parentheses are required.
- Optional curly braces − No need to use curly braces in expression body if the body contains a single statement.
- Optional return keyword − The compiler automatically returns the value if the body has a single expression to return the value. Curly braces are required to indicate that expression returns a value.
Lambda Expressions Example
Create the following Java program using any editor of your choice in, say, C:> JAVA.
Java8Tester.java
Live DemoVerify the Result
Compile the class using javac compiler as follows −
Now run the Java8Tester as follows −
It should produce the following output −
Following are the important points to be considered in the above example.
- Lambda expressions are used primarily to define inline implementation of a functional interface, i.e., an interface with a single method only. In the above example, we've used various types of lambda expressions to define the operation method of MathOperation interface. Then we have defined the implementation of sayMessage of GreetingService.
- Lambda expression eliminates the need of anonymous class and gives a very simple yet powerful functional programming capability to Java.
Scope
Using lambda expression, you can refer to any final variable or effectively final variable (which is assigned only once). Lambda expression throws a compilation error, if a variable is assigned a value the second time.
Scope Example
![Features Features](/uploads/1/2/3/9/123914478/726061824.jpg)
Create the following Java program using any editor of your choice in, say, C:> JAVA.
Java8Tester.java
Live DemoVerify the Result
Compile the class using javac compiler as follows −
Now run the Java8Tester as follows −
It should produce the following output −