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.
-
Calls to static methods in lambda expressions can become a reference to the method.
src/org/j6toj8/lambda/methodreference/MethodReference_Static.javalink:../../../src/org/j6toj8/lambda/methodreference/MethodReference_Static.java[role=include]
console output5 5
In this case the only thing the lambda expression does is take an
x
argument and pass it to theString
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 theFunction
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 theString.valueOf(x)
method. -
The lambda expression is simple: it only has one method call.
-
-
Method calls from a specific instance can also be represented as a reference to a method.
src/org/j6toj8/lambda/methodreference/MethodReference_Instance.javalink:../../../src/org/j6toj8/lambda/methodreference/MethodReference_Instance.java[role=include]
console output5 - 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 theBiFunction
functional interface (takes two arguments and returns a value). -
The input arguments of the lambda expression
x
andy
are exactly the same as those passed to theConverter.convert(Integer, Integer)
method. -
The lambda expression is simple: it only has one method call.
-
-
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.javalink:../../../src/org/j6toj8/lambda/methodreference/MethodReference_Type.java[role=include]
console output8.0 8.0
In this example, reference is being made to the
doubleValue
method ofInteger
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 anx
argument and returns adouble
value). -
The lambda expression takes an
x
argument ofInteger
type, which has thedoubleValue
method that takes no parameters. -
The lambda expression is simple: it only has one method call.
-
-
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.javalink:../../../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 ofInteger
where thecompareTo
method will be called. And thaty
is the instance ofInteger
that will be passed as an argument to thecompareTo
method. -
Calls to a constructor can also be represented as a reference to a method.
src/org/j6toj8/lambda/methodreference/MethodReference_Constructor.javalink:../../../src/org/j6toj8/lambda/methodreference/MethodReference_Constructor.java[role=include]
console output1 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.
-
Complex lambda expressions cannot be converted to method references, like the expression below:
src/org/j6toj8/lambda/methodreference/MethodReference_Complex.javalink:../../../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. -
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.javalink:../../../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.
-
-
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.javalink:../../../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.
-
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. The Java™ Tutorials.