DEV Community

Cover image for How to use Function Pointers in Java ️ ✨
Gregory Gaines
Gregory Gaines

Posted on

How to use Function Pointers in Java ️ ✨

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 method references, 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.

// Wrapping interface
private interface FunctionPointer {
  // Method signatures of pointed method
  void methodSignature(int a);
}
Enter fullscreen mode Exit fullscreen mode

Next, create methods with the target method signature.

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);
}
Enter fullscreen mode Exit fullscreen mode

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.

// 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);
Enter fullscreen mode Exit fullscreen mode
Called method1 with integer 3

Called method2 with integer 2

Called method3 with integer 5
Enter fullscreen mode Exit fullscreen mode

Using Lambdas

Method references can be assigned using lambdas.

// 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);
Enter fullscreen mode Exit fullscreen mode

Array of Function Pointers

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

// 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);
Enter fullscreen mode Exit fullscreen mode

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

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 newsletter or supporting me if this was helpful. Thanks for reading!

Top comments (0)