Hello guys, if you are new to the Java 8 style of coding and wondering what is those double colon(::) in the code then you have come to the right place. In this article, I will explain to you the double colon operator and what it does in Java 8. The double colon (::) operator is known as the method reference in Java 8. Method references are expressions that have the same treatment as a lambda expression, but instead of providing a lambda body, they refer to an existing method by name. This can make your code more readable and concise.
For example, to print all elements of the list, you can use the forEach() method as follows:
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 to a static method, an instance method, or a constructor. For referring to a static method, you can either use className or instanceRef, but to refer to an instance method of a particular object, you need to use instanceRef.
Where can you use the double colon operator in Java?
You can use the double colon operator (::) wherever you need to use the method reference. Here are some examples of a method reference in Java 8:- A static method (ClassName::methodName) like Person::getAge
- An instance method of a particular object (instanceRef::methodName) like System.out::println
- A super method of a particular object (super::methodName)
- An instance method of an arbitrary object of a particular type (ClassName::methodName)
- A class constructor reference (ClassName::new) like ArrayList::new
- An array constructor reference (TypeName[]::new) like String[]::new
The idea is that if you are passing a lambda expression, like a function that takes a value and prints in the console then instead of giving lambda expression, just pass the println() method of PrintStream class which you can access as a System.out::println.
Remember, there is no parenthesis when you refer to a method using method reference or lambda expression in Java 8. If you want to learn more, then these Java collections and Stream courses are the best places to start with.
Real-world Example of Double Colon (::) Operator in Java 8
Here is a real-world example of method reference using a double colon operator in Java 8. In this example, we are creating a Comparator that compares Person objects 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 a double colon operator or method reference.You can see that the second example is much more readable and clean:
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 a double colon operator.
And, if you want to learn lambda expression and method reference from scratch then I recommend you to check out this Java Functional Programming using the Lambdas and Stream course for Beginners and experienced programmers.
That's all about the double colon operator of Java 8. As I said, it's known as method reference and can be used to refer to 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 cleaner code.
Related Java 8 Tutorials
If you are interested in learning more about the new features of Java 8, here are my earlier articles covering some of the essential concepts of Java 8:- The Java Developer RoadMap (see)
- 5 Books to Learn Java 8 from Scratch (books)
- How to join String in Java 8 (example)
- 5 Courses to learn Java for Beginners (courses)
- How to use filter() method in Java 8 (tutorial)
- 20 Examples of Date and Time in Java 8 (tutorial)
- Top 5 Courses to learn Functional Programming in Java (courses)
- 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)
- Top 5 courses to become a full-stack Java developer (courses)
- 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 it with your friends and colleagues. If you have any questions or feedback, then please drop a note.
P. S. - If you want to learn more about new features in Java 8 then please see these free Java Programming courses. It explains all the important features of Java 8 like lambda expressions, streams, functional interfaces, Optional, new Date Time API, and other miscellaneous changes
I found this feature to be confusing at least. Also it works while everything is ok, but when there is exception the size of the exception stack is huge. I don't like it. I don't use it in my code.
ReplyDelete