Lambda Expressions with Parameters

Java-Lambda Expressions with Parameters

Lambda Expressions with Parameters

Following program shows the use of a parameter with a lambda expression. The two parameters, n and v, are specified in the parameter list, separated by commas(first line in the main()). This example can be generalized. Whenever more than one parameter is required, the parameters are specified, separated by commas, in a parenthesized list on the left side of the lambda operator. Notice that the type of n and v is not specified. Rather, its type is inferred from the context. In this case, its type is inferred from the parameter type of nameAndVersion(String name, int version) as defined by the Programming interface, which is String and int. It is also possible to explicitly specify the type of a parameter in a lambda expression.

Lambda Expressions with Parameters


Lambda Expressions with Parameters Private Interface Methodss

Program Source

interface Programming {
    String nameAndVersion(String name,int version);
public class Javaapp {

    public static void main(String[] args) {
        Programming language =(n,v)->"Language : "+n+", Version : "+v;
        String str1 = language.nameAndVersion("Java", 10);
        language =(String n,int v)->"Language : "+n+", Version : "+v;
        String str2 = language.nameAndVersion("PHP", 7);

Leave a Comment