Skip to content

Latest commit

 

History

History
171 lines (150 loc) · 7 KB

04-method-reference.asc

File metadata and controls

171 lines (150 loc) · 7 KB

Method Reference

Objective
Develop code that uses a method reference, including refactoring a lambda expression to a method reference.

The reference syntax for a method is new to Java 8. With it you can reference specific methods on four different occasions:

  • Static Method References → String::valueOf

  • Method references of an object → instanceString::isEmpty

  • References to methods of an object type (of a class, interface, etc.) → String::isEmpty

  • Constructor References → String::new

It is essential to remember Functional Interfaces, Lambda Expression syntax variations, and Built-in Interfaces definitions. If necessary, review the sections in this chapter.

You can think of method references as another way to write a lambda expression if the only thing your lambda expression does is call another method.

The following are the occasions when method references are used.

  1. Calls to static methods in lambda expressions can become a reference to the method.

    src/org/j6toj8/lambda/methodreference/MethodReference_Static.java
    link:../../../src/org/j6toj8/lambda/methodreference/MethodReference_Static.java[role=include]
    console output
    5
    5

    In this case the only thing the lambda expression does is take an x argument and pass it to the String valueOf method. To simplify this, Java 8 allows you to write this same lambda function as presented on the following line: String::valueOf.

    You can only represent the first lambda expression in the form of a method reference because:

    • The implementation of String.valueOf satisfies the Function functional interface (takes an argument and returns a value).

    • The input argument of the lambda x expression is exactly the same as that passed to the String.valueOf(x) method.

    • The lambda expression is simple: it only has one method call.

  2. Method calls from a specific instance can also be represented as a reference to a method.

    src/org/j6toj8/lambda/methodreference/MethodReference_Instance.java
    link:../../../src/org/j6toj8/lambda/methodreference/MethodReference_Instance.java[role=include]
    console output
    5 - 8
    5 - 8

    You can only represent the first lambda expression in the form of a method reference because:

    • The implementation of Converter.convert(Integer, Integer) satisfies the BiFunction functional interface (takes two arguments and returns a value).

    • The input arguments of the lambda expression x and y are exactly the same as those passed to the Converter.convert(Integer, Integer) method.

    • The lambda expression is simple: it only has one method call.

  3. Calls to methods of a class, without specifying the specific instance, can also be represented as a reference to a method.

    src/org/j6toj8/lambda/methodreference/MethodReference_Type.java
    link:../../../src/org/j6toj8/lambda/methodreference/MethodReference_Type.java[role=include]
    console output
    8.0
    8.0

    In this example, reference is being made to the doubleValue method of Integer type. You can only represent the first lambda expression in the form of a method reference because:

    • Our lambda expression satisfies the Function functional interface (takes an x argument and returns a double value).

    • The lambda expression takes an x argument of Integer type, which has the doubleValue method that takes no parameters.

    • The lambda expression is simple: it only has one method call.

  4. You can also use the method reference of a type, as in the previous example, even if the method receives parameters.

    src/org/j6toj8/lambda/methodreference/MethodReference_TypeWithParam.java
    link:../../../src/org/j6toj8/lambda/methodreference/MethodReference_TypeWithParam.java[role=include]
    console output
    -1
    -1

    In this example the compiler "discovers" even more things than in the previous examples. By writing only the method reference, the compiler understands that the x variable, which comes first, will be the instance of Integer where the compareTo method will be called. And that y is the instance of Integer that will be passed as an argument to the compareTo method.

  5. Calls to a constructor can also be represented as a reference to a method.

    src/org/j6toj8/lambda/methodreference/MethodReference_Constructor.java
    link:../../../src/org/j6toj8/lambda/methodreference/MethodReference_Constructor.java[role=include]
    console output
    1
    1

    This example is very similar to the previous one, with the only difference being that the referenced method is a constructor. Note that the constructor also gets a parameter and, as in the previous example, the compiler understands that the lambda function argument must be passed to the called constructor.

  6. Complex lambda expressions cannot be converted to method references, like the expression below:

    src/org/j6toj8/lambda/methodreference/MethodReference_Complex.java
    link:../../../src/org/j6toj8/lambda/methodreference/MethodReference_Complex.java[role=include]

    Since in this case we have another String + "2" being added to the constructor, there is no way to represent this with a simple reference to the constructor.

  7. You can use method reference also with your own classes. See the following example for types created by our code and equivalent lambda expressions with and without reference to methods.

    src/org/j6toj8/lambda/methodreference/MethodReference_CustomType.java
    link:../../../src/org/j6toj8/lambda/methodreference/MethodReference_CustomType.java[role=include]

    Note the difference between lambda expressions:

    • One part implements the Function functional interface, as they take an argument and return a value.

    • The latter implements the Supplier functional interface, as it does not take an argument, but returns a value.

      If in doubt, refer back to the types of functional interfaces in the other sections of this chapter.

  8. The variety of ways to represent the same lambda expression can be large, so be careful not to get confused.

    src/org/j6toj8/lambda/methodreference/MethodReference_Variaty.java
    link:../../../src/org/j6toj8/lambda/methodreference/MethodReference_Variaty.java[role=include]

    You have seen all the ways to create a lambda expression, from the most complete to the simplest. Make sure you know all these variations for the certification exam.

References
  • Using Method References

    Boyarsky, Jeanne; Selikoff, Scott. OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide (p. 152). Wiley. Kindle Edition.

  • Method References in Java.

  • Method References. The Java™ Tutorials.