How to Add Buttons and Text to Your JavaFX Project
Java: Using Relational and Conditional Operators
How to Use Blocks in Java

How to Use Lambda Expressions in Java 8

Java 8 introduces a new feature that in some ways is similar to anonymous classes, but with more concise syntax. More specifically, a lambda expression lets you create an anonymous class that implements a specific type of interface called a functional interface— which has one and only one abstract method.

The Ball interface meets that definition:

interface Ball
{
 void hit();
}

Here the only abstract method is the hit method.

A functional interface can contain additional methods, provided they are not abstract. Until Java 8, this was not possible because an interface could contain only abstract methods. However, in Java 8 you can create default methods which provide a default implementation. Thus a functional interface can contain one or more default methods, but can contain only one abstract method.

A lambda expression is a concise way to create an anonymous class that implements a functional interface. Instead of providing a formal method declaration that includes the return type, method name, parameter types, and method body, you simply define the parameter types and the method body. The Java compiler infers the rest based on the context in which you use the lambda expression.

The parameter types are separated from the method body by a new operator, called the arrow operator, which consists of a hyphen followed by a greater-than symbol. Here’s an example that implements the Ball interface:

() -> { System.out.println("You hit it!");}

Here the lambda expression implements a functional interface whose single method does not accept parameters. When the method is called, the text "You hit it!" is printed.

You can use a lambda expression anywhere you can use a normal Java expression. You’ll use them most in assignment statements or as passed parameters. The only restriction is that you can use a lambda expression only in a context that requires an instance of a functional interface. For example, here’s a complete program that uses a lambda expression to implement the Ball interface:

public class LambdaBall
{
 public static void main(String[] args)
 {
  Ball b = () -> { System.out.println("You hit it!"); };
  b.hit();
 }
 interface Ball
 {
  void hit();
 }
}

The general syntax for a lambda expression is this:

(parameters) -> expression

or this:

(parameters) -> { statement; ... }

If you use an expression, a semicolon is not required. If you use one or more statements, the statements must be enclosed in curly braces and a semicolon is required at the end of each statement.

Don’t forget that the statement in which you use the lambda expression must itself end with a semicolon. Thus, the lambda expression in the previous example has two semicolons in close proximity:

Ball b = () -> { System.out.println("You hit it!"); };

The first semicolon marks the end of the statement that calls System.out.println; the second semicolon marks the end of the assignment statement that assigns the lambda expression to the variable b.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Logical Operators in Java
JavaFX: How to Create Reflections
How to Create a JTree Component in Java
Use Array Lists in Java
Programming Java: Throwing Errors
Advertisement

Inside Dummies.com