:::: MENU ::::

Saturday, November 25, 2023

Java can achieve callback paradigm using functional interface. It's widely used in APIs. Java developer are fortunate to have these. Lets jump into it.

First of all we have to know about what is callback function. According to wikipedia:


In computer programming, a callback or callback function is any reference to executable code that is passed as an argument to another piece of code; that code is expected to call back (execute) the callback function as part of its job. This execution may be immediate as in a synchronous callback, or it might happen at a later point in time as in an asynchronous callback. They are also called blocking and non-blocking.

So in plain language, call back is passed in another function's arguments. That call back is called when it's required to call from. Java hasn't familiar with callback functions in the same way as JavaScript,primarily due to differences in their programming paradigms.

However, Java manages itself to achieve similar functionality. Using functional interface it's possible to achieve in java. Do you know it comes from java 8!!!

Let's back to the coding example:

What is functional interface? "A functional interface is an interface that contains only one abstract method.", according to geeksforgeeks. Example DataProcessor class which is used to demostrate callback function.
Code snippet:


  //define a functional interface for the callback
  public interface Callback {
  void callback(String result);
}

// Class that uses the callback
class DataProcessor {
    void processData(String data, Callback callback) {
        // Simulate processing data
        String result = "Processed: " + data.toUpperCase();

        // Invoke the callback
        callback.onCallback(result);
    }
}

public class CallbackExample {
    public static void main(String[] args) {
        DataProcessor dataProcessor = new DataProcessor();

        // Use an anonymous class as a callback
        dataProcessor.processData("Hello, World!", new Callback() {
            @Override
            public void onCallback(String result) {
                System.out.println("Callback result: " + result);
            }
        });

        // Using lambda expression for the callback
        dataProcessor.processData("Java Callback", result -> {
            System.out.println("Lambda Callback result: " + result);
        });
    }
}
In this example:

- The Callback interface defines a single method onCallback.

- The DataProcessor class takes an instance of the Callback interface as a parameter to its processData method.

- In the main method, an anonymous class and a lambda expression are used to provide different implementations of the callback.

While Java doesn't have the exact syntax for callback functions as in JavaScript, the use of interfaces, anonymous classes, and lambda expressions provides a flexible way to achieve similar behavior. This approach is commonly used in Java APIs and libraries.


Do you know java functional programming, Supplier, and Consumer interfaces??

0 comments:

Post a Comment