What is Callback, Callback in Java

Tram Ho

Hello everyone, today I will introduce you to “callback” in java. So first let’s find out what callback is and the effect of callback first

What is I.Callback

Oki in the example always makes it hot.

(I am afraid that some of you are not familiar with javascript code, I cmt a bit detailed for easy visualization)

As the code below, I display the even numbers in the array 1 array. So if I want to show one of the numbers now, what should I do? What if I want to write another function, or if I want to display more complex conditions ??? And callback was born to solve this problem

Take a look at the code. Instead of permanently assigning the code that checks for even numbers. I pass a function straight into the function. The passed function will perform the test for itself. And will be defined outside Oki seems a bit chaotic. Or try to take a more realistic example to make it easier to visualize.

  • For example, you are a creditor, you have a list of creditors and you need to collect. (array)
  • Instead you have to go to the debtor’s house to claim (first code).
  • You will ask another guy. You give it the home address (data – pass the ith element).
  • It asks in any way you don’t need to know. just know whether to claim it or not. If you are hungry, delete the name from the debt list (the code is displayed).
  • If the debtor can easily use the medium guy, If the debtor has difficulty using the big guy (flexible use depending on the case like wanting to filter even or odd numbers)

According to wiki

“The callback is an executable code (usually a function A) that is used as an argument to the function B. Function A is called immediately or a little late after it has been called. Different programming languages ​​support callbacks in different ways, often implemented as subroutines, anonymous functions, command strings or function pointers. ”

So simple visualization of the callback is passing a function to another function, easier to understand, right.

I will introduce two more ways to write callbacks to be able to easily use callbacks in javascript

With two implementations, it’s easy to dynamically implement the callback without having to create a specific function. So through a small example in the javascript language you probably understand the basic what the callback is and the use of the callback. Now let’s go to the main step, implement the callback in java

II. Callback in java

On the main topic of this day. So can java implement callback like javascript?

As we all know, Java is a pure Object Oriented language. You can only pass primitive data types (int, long, double, …) or Class (List, String, ..) or Class defined.

=> java has no callback? Not really good see the following example.

Measuring the code solves the problem like javascript

If Java does not allow passing a function, then try passing a class with only one method. Probably not, now want to do it with odd numbers. Looks like this is still not very good.

The goal of the callback is that function A will be passed to function B. and function A will be defined when calling function B. We need something just declare the function name and will be done later, sounds familiar? ?? Boom!!!

Right. Or try the interface. Interface only needs to implement the function when deployed. It is now possible to easily deploy even odd numbers by creating different instances of the KiemTra interface

Of course you can create a class KiemTraSoLe impl KiemTra. Implementing Polymorphism in java to pass parameters, but it’s too inconvenient

Done … got call in java

So Java uses Interface mechanism to implement callback. With a condition that the interface has only one method (there are 2 methods, we know who to call ….).

But implementing callbacks in java seems a bit long, right?

It is possible to initialize the interface in parameters to make the code shorter. Specifically in Java 8 also provides a lambda mechanism for easy implementation of CallBack.

The code has been pulled off a lot and looks more like js. About lambda I will have my own article combined with the use of Stream API to speed up code implementation with Collection.

In addition, java 8 also provides a lot of interfaces combined with generics to easily implement lambda rendering. For example: Function where T is the input parameter, R is the output

  • Consumer has a void
  • Predicate has a boolean return type,
  • IntFunction, LongFunction, DoubleFunction have Int, Long, and Double return types.
  • And many other interfaces I will introduce and apply in the next article about lambda and streamAPI
Share the news now

Source : Viblo