A Guide to Method References in Java 8

A Guide to Method References in Java 8

In this blog, we will get to know about method references in Java, where and how to use them. What are Method References? Method References allow us to pa

In this blog, we will get to know about method references in Java, where and how to use them.

What are Method References?

Method References allow us to pass the name of a method where a functional interface is expected.

This brings us to another question.

What is a Functional Interface?

Functional Interfaces are single-method interfaces that encapsulate a single behaviour. For eg. Runnable which has a single method run(). The standard library contains many built-in functional interfaces that you can use inside the java.util.function package.

Following is the general syntax of a functional interface.

@FunctionalInterface
interface MyInterface{
  ReturnType methodName(FunctionParameters){
    FunctionBody
  }
}

Now that we know about Functional Interfaces too, it’s time to learn how to use method references.

How are Method References used?

As told previously, method references are used to pass the name of the method where a functional interface is expected. We use the following syntax to refer to a method on a particular instance of a class.

instanceReference::methodName

Note: The type signature of the referenced method and that of the method in the functional interface must match.

Let’s say the method that is to be passed is this.

public void printUser(User user){
  System.out.println(user.toString());
}

functional programming java method references

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

The new aspect of Java with functional programming

Functional programming is a well-known concept in Javascript and Python. But, it’s a fancy thing for a java developer. Prior to Java 8, Java has been a pure OOP where objects are first-class citizens.

Functional Programming Techniques in Java with Examples

In this article, I will write about how to write code using Java’s API for functional programming. In the end, we will write our own stream API so we can understand how to implement a functional programming style in Java.

Method Overriding in Java Example | Java Method Overriding

Method overriding in java can be defined as a method in a subclass or child class that is already present in the superclass. That means the method of the subclass is having the same name, same parameter, and the same return type as the method of its superclass.

Functional Programming

Functional Programming: Functional Programming is a Declarative style of Programming Paradigm for writing computer programs.