Omitting Parameter Types

Description

We can choose to omit parameter types in lambda expressions.

In lambda expression (int x, int y) -> { return x + y; } the types of parameters are declared.

We can safely rewrite the lambda expression by omitting the types of parameters as

(x, y) -> { return x + y; }

If we choose to omit the types of parameters, we have to omit types for all parameters.

Example


public class Main {
  public static void main(String[] argv) {
    Processor stringProcessor = (str) -> str.length();
    String name = "Java Lambda";
    int length = stringProcessor.getStringLength(name);
    System.out.println(length);//from w w  w  .  j  av  a  2s  .c  o m

  }
}

@FunctionalInterface
interface Processor {
  int getStringLength(String str);
}

The code above generates the following result.





















Home »
  Java Lambda »
    Java Lambda Tutorial »




Java Lambda Tutorial