What is double colon (::) operator in Java 8 - Example

The double colon (::) operator is known as the method reference in Java 8. Method references are expressions which have the same treatment as a lambda expression, but instead of providing a lambda body, they refer to an existing method by name. For example, to print all elements of the list you can use the forEach() method as follows

list.stream.forEach( s-> System.out.println(s));

but here you can replace lambda expression with method reference to improve readability as shown below:

list.stream.forEach( System.out::println);

You can use method reference or double colon operator to refer a static method, an instance method, or a constructor.

For referring a static method, you can either use className or instanceRef, but to refer an instance method of a particular object, you need instanceRef.

Here are some examples of method reference in Java 8:

  1. A static method (ClassName::methodName) like Person::getAge
  2. An instance method of a particular object (instanceRef::methodName) like System.out::println
  3. A super method of a particular object (super::methodName)
  4. An instance method of an arbitrary object of a particular type (ClassName::methodName)
  5. A class constructor reference (ClassName::new) like ArrayList::new
  6. An array constructor reference (TypeName[]::new) like String[]:new 

The idea is that if you are passing a lambda expression e.g. a function which takes a value and prints in the console than instead of passing lambda expression, just pass the println() method of PrintStream class which you can access as System.out::println.

Remeber there is no parenthesis when you refer a method using method reference or lambda expression in Java 8.  If you want to learn more, The Complete Java MasterClass is a good place to start with.

Real world Example of Double Colon Operator in Java 8

Here is a real-world example fo method reference using double colon operator in Java 8. In this example, we are creating a Comparator which compares person by their age. In the first example, we have used a lambda expression to pass the logic to compare age, while in the second line of code we have used double colon operator or method reference.

You can see that the second example is much more readable and clean:

What is double colon (::) operator in Java 8 - Example

Btw, If you are using IDE like Eclipse, NetBeans, or IntelliJ IDEA, the IDE will also suggest you convert your lambda expression to method reference as shown in the following screenshot.

This confirms that it's a best practice in Java 8 to convert lambda expression to a method reference or constructor reference using double colon operator. See this articles for more tips on when and how you can convert a lambda expression to method reference in Java 8.

double colon (::) operator in Java 8 - Example

That's all about double colon operator of Java 8. As I said, it's known as method reference and can be used to refer an existing method by name instead of using a lambda expression.  It's a  Java 8 best practice to favor method reference over lambda expression as it results in more cleaner code.

Further Reading
What's New in Java 8
The Ultimate Java 8 Tutorial
Java SE 8 New Features

Related Java 8 Tutorials
If you are interested in learning more about new features of Java 8, here are my earlier articles covering some of the important concepts of Java 8:

  • How to join String in Java 8 (example)
  • How to use filter() method in Java 8 (tutorial)
  • 20 Examples of Date and Time in Java 8 (tutorial)
  • How to use Stream class in Java 8 (tutorial)
  • How to sort HashMap in Java 8? (example)
  • How to use forEach() method in Java 8 (example)
  • 5 Books to Learn Java 8 from Scratch (books)
  • How to convert List to Map in Java 8 (solution)
  • How to use peek() method in Java 8 (example)
  • 10 Java 8 Tutorials You must read (tutorials)

Thanks for reading this article so far. If you like this article then please share with your friends and colleagues. If you have any questions or feedback then please drop a note. 

No comments:

Post a Comment