Introduction to method references in Java 8 with examples

Introduction to method references in Java 8

Method references help to refer to methods by their names. Method references can be used as an argument for a matching functional interface (An interface with only one method is called a functional interface). A method reference is described using :: (double colon) symbol. Method reference is one of the features belonging to Java lambda expressions. Sometimes, a lambda expression does nothing but call an existing method. In those cases we can use method reference to refer to the existing method by name.

Method references and Lambda expressions are similar in a way that they both require a target type that consists of a compatible functional interface. The method in the functional interface and the passing method reference should match for the argument and return type.

There are four kinds of method references:

  • Reference to a static method. Syntax –> ClassName :: staticMethodName
  • Reference to an instance method of a particular object. Syntax –> containingObject::instanceMethodName
  • Reference to an instance method of an arbitrary object of a particular type. Syntax -> ContainingType::methodName. e.g. String::compareToIgnoreCase
  • Reference to a constructor. Syntax -> ClassName::new. e.g. HashSet::new

 

Reference to a static method

Static methods can be referred using the syntax ClassName :: staticMethodName. Let us first see an example using lambda expression and then the same using method reference.

 

Now consider if there is already an implementation for the method print. Now we can write the same program using method reference as below.

 

Reference to an instance method of a particular object

Instance methods can be referred using the syntax containingObject::instanceMethodName. Consider an example below.

 

Another simple example of this would be,

numbers.forEach(System.out::println); // to print each number

Here out is an object of PrintStream class and println is an instance method in it.

 

Reference to an instance method of an arbitrary object of a particular type

Instance method of an arbitrary object of a particular type can be referred using the syntax ContainingType::methodName.

 

Here it should be noted that display is an instance method which will be called for each object of type Person in the list. You might know the fact that instance methods can be called only on an object reference. Here the object reference is an arbitrary one.

The same statement(#14) can be written using lambda expression as follows,

persons.forEach(person->System.out.println(“Name: “+person.getName() + ” Age: “+person.getAge()));

Reference to a constructor

The constructor can be referred using the syntax ClassName::new

Let us take an example using lambda expression.

 

Here the getInteger() method takes a String argument and returns an Integer object. Isn’t it the same functionality done by the Integer constructor? Yes it is. So the same program can be written using constructor reference as below.

 

So in this case, the integer constructor becomes an implementation for the getInteger() method.

Summary

  • You can use Method References in place of lambda expression where the implementation replicates an already existing method.
  • If you would like to use an already existing method as an implementation for a functional interface, you could use method reference instead of lambda expression.
  • Method references can only be used in places where a functional interface is expected.

 

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

Required fields are marked *. Your email address will not be published.