Method Reference in Java 8
Method references are a new feature of Java 8. They are a short and easily readable writing syntax for a lambda expression. Use a method reference to refer to a functional interface method. You can replace a method reference for your lambda expression whenever you just refer to a method with a lambda expression.
Using the article, let's learn more about this new feature of a method reference in Java 8 in-depth.
Introduction to Method Reference in Java 8
Generally, a method represents a particular way of accomplishing or approaching something. In the same way in programming, methods are a group of codes designed to perform a task. It makes the code reusable and readable.
A method gets executed upon calling. Each method has various components like an access specifier, return type, method name, parameters list, and method body. It is of two kinds - user-defined or pre-defined methods.
The above code is an example of a method created inside the Main class named myMethod. The static keyword used with the name restricts the methods' access through objects. The void here specifies the return type, as the current method does not return any value.
Java is always an object-oriented programming language where everything starts and ends around the objects.
A functional interface has only one abstract or unimplemented method other than different default and static methods. It exhibits only a single functionality.
Lambda expressions are the expression of instances used in functional interfaces. These are only available from Java 8 onwards. Method references refer to the method of functional interface. It is an easy way of writing lambda expressions used to call a method.
Let's understand it better with the help of an example.
Consider a lambda expression that is as follows:
By method reference, it will change to this:
In method reference, the :: operator divides the method name from the class or object name.
In the statement System.out::println, System is defined in the java.lang package, out is an instance of the type PrintStream, which is a public and static member field of the System class, and println() is a public method of all instances of the PrintStream class.
Types of Method Reference in Java 8
There are four types of method references. They are as follows:
1. Method reference to an instance method of a particular object of a class
This type of method reference refers to non static instance methods by a class object.
Lambda expression for method reference to an instance method of a particular object of a class is:
The above lambda expression can get replaced by a method reference. The shorted code looks like this: object::instanceMethod
Example
Output
In the above example, we have defined a functional interface named display. Here, the method myMethod is referred to by the object of the class Example. It then calls the method inside the functional interface Display.
2. Method reference to the static method of a class
This method reference is used to refer to a static method that is defined in a class.
Lambda expression for method reference to a static method of a class is:
The above lambda expression can get replaced by a method reference. The shorted code looks like this:
Example
Output
In the above example, we have used a pre-defined functional interface instead of a user-defined one. The BiFunction Interface refers to the method named Multiplication to find the product of the two numbers.
3. Method reference to an instance method of an arbitrary object of a specific type
This type of method reference refers to non static instance methods without creating a custom object. It uses an anonymous object to refer to the instance method.
Lambda expression for method reference to an instance method of an arbitrary object of a specific type is:
The above lambda expression can get replaced by a method reference. The shorted code looks like this:
or
Example
Output:
In the above example, we take the reference to an arbitrary object of a specific type. There is no need for a custom object to perform the comparison. Here the sort method sorts the stringArray that arranges the names in the lexicographical order.
4. Method reference to a constructor
This type of method reference refers to a constructor by using the new keyword.
Lambda expression for method reference to a constructor of a class is:
The above lambda expression can get replaced by a method reference. The shorthand code looks like this:
Example
Output:
Method Reference to a constructor in java is different from method reference to a static or instance method. Method reference to a constructor is using the new keyword. The keyword helps the method Scalar to refer to the constructor using the keyword.
Conclusion
- Method Reference in java 8 makes the code simple and more readable than lambda expression.
- Method reference refers to the method via the use of an :: operator.
- A method reference in Java 8 can execute only a single method call like a lambda expression but a shorter code.
- Java 8 allows the method reference to static methods, instance methods, and constructors.