An Interest In:
Web News this Week
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
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 ofmethod 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 interfaceprivate interface FunctionPointer { // Method signatures of pointed method void methodSignature(int a);}
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);}
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 referencesFunctionPointer pointer1 = this::method1;FunctionPointer pointer2 = this::method2;// Call both methods using their "pointer"pointer1.methodSignature(3);pointer2.methodSignature(2);// Reassign and call pointer 1pointer1 = this::method3;pointer1.methodSignature(5);
Called method1 with integer 3Called method2 with integer 2Called method3 with integer 5
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);
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 methodsfunctionPointersArray[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.
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!
Original Link: https://dev.to/gregorygaines/how-to-use-function-pointers-in-java-3f33
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To