Lambda Expressions as Arguments

Java-Lambda Expressions as Arguments

Lambda Expressions as Arguments

A lambda expression can be used in any context that provides a target type. One of these is when a lambda expression is passed as an argument. In fact, passing a lambda expression as an argument is a common use of lambdas. Moreover, it is a very powerful use because it gives you a way to pass executable code as an argument to a method. This greatly enhances the expressive power of Java.

To pass a lambda expression as an argument, the type of the parameter receiving the lambda expression argument must be of a functional interface type compatible with the lambda. Although using a lambda expression as an argument is straightforward, it is still helpful to see it in action. The following program demonstrates that process, first notice the showStudentMark( ) method. It has two parameters. The first is of type StudentMark, which is a functional interface. Thus, this parameter can receive a reference to any instance of StudentMark, including one created by a lambda expression. The second argument of showStudentMark( ) is of type String.

Next, notice the first and second call to showStudentMark( ), a simple expression lambda is passed as an argument. When this occurs, an instance of the functional interface StudentMark is created and a reference to that object is passed to the first parameter of showStudentMark( ). Thus, the lambda code, embedded in a class instance, is passed to the method. The target type context is determined by the type of parameter. Because the lambda expression is compatible with that type, the call is valid. Embedding simple lambdas, inside a method call is often a convenient technique—especially when the lambda expression is intended for a single use. Next, the program passes a block lambda to showStudentMark( ), the process of passing the lambda expression is the same as just described for the simple expression lambda.

When a block lambda seems overly long to embed in a method call, it is an easy matter to assign that lambda to a functional interface variable. Then, you can simply pass that reference to the method. This technique is shown at the end of the program. There, a block lambda is defined and assigned to st, which is a reference to a StudentMark instance. Thus, st can be used as an argument to the first parameter of showStudentMark( ). The program then calls showStudentMark( ), passing in st. Because the instance obtained by the evaluation of each lambda expression is an implementation of StudentMark, each can be used as the first parameter to showStudentMark( ).


Lambda Expressions as ArgumentsLambda Expressions as Arguments

Program Source

interface StudentMark {
    int getMark();
class Javaapp {
    static void showStudentMark(StudentMark st,String grade)
    public static void main(String[] args) {
        showStudentMark(()->85," : Grade 2");
        showStudentMark(()->70," : Grade 3");
            System.out.print("First Class Mark->");
            return 98;
        }," : Grade 1");
        StudentMark st = ()->50;
        showStudentMark(st," : Grade 4");

Leave a Comment