How to use Function Pointers in Java ️ ✨
Tags: java, tutorial, programming, beginners
Published: 2022-04-05 02:35:44

# Introduction
Pointers are objects that store a memory address and can save memory by directly pointing to a target object, array, or variable address instead of passing by value. Sadly Java doesn't have a "real" concept of pointers. Lucky for us, there's a workaround using method references that comes close to the real thing.

# Function Pointers
A function pointer is a pointer that points to the address of a function. Some uses are creating a callback routine by creating a function that calls another function based on its execution or storing an array of function pointers for dynamically calling methods (Ex. Storing CPU instructions for an emulator).

# Simulating a Function Pointer
There are four kinds of <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html" target="_blank" rel="noopener">method references</a>, and we are using the kind that refers to an instance method of a particular object.

Start by defining an interface that defines the method signatures of the method(s) you are pointing to.

```java
// Wrapping interface
private interface FunctionPointer {
  // Method signatures of pointed method
  void methodSignature(int a);
}
```

Next, create methods with the target method signature.

```java
public void method1(int b) {
  System.out.println("Called method1 with integer " + b);
}

public void method2(int v) {
  System.out.println("Called method2 with integer " + v);
}

public void method3(int a) {
  System.out.println("Called method3 with integer " + a);
}
```

The next step is to create variables of the wrapping interface and assign methods to them. The variables will act as a function pointer to be stored or executed.

```java
// Create a variable of the interface and assign
// the method references
FunctionPointer pointer1 = this::method1;
FunctionPointer pointer2 = this::method2;

// Call both methods using their "pointer"
pointer1.methodSignature(3);
pointer2.methodSignature(2);

// Reassign and call pointer 1
pointer1 = this::method3;

pointer1.methodSignature(5);
```

```markdown
Called method1 with integer 3

Called method2 with integer 2

Called method3 with integer 5
```

# Using Lambdas
Method references can be assigned using lambdas.

```java
// Create a method reference and assign a methods using a lambda.
FunctionPointer pointer1 =
  (a) -> System.out.println("Called pointer1 with int " + a);

FunctionPointer pointer2 =
  (b) -> System.out.println("Called pointer2 with int " + b);
```

# Array of Function Pointers
The functionality of an array of method references can be emulated by creating an array of the wrapping interface.

```java
// Create an array of "pointers"
FunctionPointer[] functionPointersArray = new FunctionPointer[3];

// Assign methods
functionPointersArray[0] = this::method1;
functionPointersArray[1] = this::method2;
functionPointersArray[2] = this::method3;

// Call methods    
functionPointersArray[0].methodSignature(3);
functionPointersArray[1].methodSignature(4);
functionPointersArray[2].methodSignature(5);
```

# Function Pointer vs. Direct Call
You may be wondering, is there any penalty for using a method reference over directly calling a method? The answer is yes, but a tiny one.

![Function Pointer vs. Direct Call](https://i.imgur.com/AeTGnah.png)

From the chart above, a direct method call is almost 5x faster than using a method reference because of the extra step to call the lambda than the stored method. Honestly, you probably won't notice the performance penalty, so don't worry about it.

# Conclusion
Pointers are variables that point directly to the address of objects instead of passing by value. A function pointer directly points to the address of a function which can decrease memory consumption. Java doesn't have pointers, but can emulate the behavior using method references or lambdas. Using a method reference is slower than a direct method call but not enough to deter usage of the feature.

Consider signing up for my <a href="https://gregorygaines.substack.com/subscribe">newsletter</a> or <a href="https://www.gregorygaines.com/support/">supporting me</a> if this was helpful. Thanks for reading!