What is a callback in java

Possible duplicate:
What is a callback function?

I read the definition of wikepedia callback, but I still don't understand it. Someone please explain to me that the callback, in particular, the line "In computer programming, the callback is a reference to the executable code or a piece of executable code that is passed as an argument to another code. This allows the lower level program level to call the subroutine ( or function) defined at a higher level. ".

+46
java
Jan 05 '12 at 1:11
source share
6 answers

Maybe an example will help.

Your application wants to download a file from a remote computer and then burn it to a local drive. A remote computer is the other side of the modem connection and satellite communications. The delay and transmission time will be huge, and you have other things to do. So, you have a function / method that will write a buffer to disk. You pass a pointer to this method in your network API along with the remote URI and other materials. This network call returns “immediately” and you can do other things. After 30 seconds, the first buffer from the remote computer enters the network layer. Then the network layer calls the function that you passed during the installation, and therefore the buffer is written to disk - the network layer “called back”. Note that in this example, the callback will occur on a network layer stream than the original stream, but it does not matter - the buffer is still being written to disk.

+46
Jan 05 '12 at 1:30
source share

Callbacks are most easily described in terms of the telephone system. Calling a function is similar to calling someone on the phone, asking her a question, receiving an answer, and suspending it; adding a callback changes the analogy, so by asking her a question, you will also give her your name and number so she can call you back with an answer.

Paul Yakubik, Implementing a callback in C ++.

+132
Jan 05 2018-12-12T00:
source share

A callback is the code that you pass to this method so that it can be called later.

In Java, one obvious example is java.util.Comparator . Usually you do not use Comparator directly; rather, you pass it to some code that calls Comparator at a later time:

Example:

 class CodedString implements Comparable<CodedString> { private int code; private String text; ... @Override public boolean equals() { // member-wise equality } @Override public int hashCode() { // member-wise equality } @Override public boolean compareTo(CodedString cs) { // Compare using "code" first, then // "text" if both codes are equal. } } ... public void sortCodedStringsByText(List<CodedString> codedStrings) { Comparator<CodedString> comparatorByText = new Comparator<CodedString>() { @Override public int compare(CodedString cs1, CodedString cs2) { // Compare cs1 and cs2 using just the "text" field } } // Here we pass the comparatorByText callback to Collections.sort(...) // Collections.sort(...) will then call this callback whenever it // needs to compare two items from the list being sorted. // As a result, we will get the list sorted by just the "text" field. // If we do not pass a callback, Collections.sort will use the default // comparison for the class (first by "code", then by "text"). Collections.sort(codedStrings, comparatorByText); } 
+22
Jan 05 2018-12-12T00:
source share

The callback is commonly used in asynchronous programming, so you can create a method that processes the response from the web service. When you call a web service, you can pass a method to it so that when the web service answers, it will call the method that you told it ... it “calls back”.

In Java, this can usually be accomplished by implementing an interface and passing an object (or an anonymous inner class) that implements it. You often come across transactions and flows - for example, the futures API.

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Future.html

+13
Jan 05 2018-12-01T00:
source share

The concept of callback function does not exist in Java, because in Java there are no functions, only object methods. However, there are situations when you can talk about a callback object or callback interface .

For example, when you call an ArrayList object to sort, and you supply a comparator that knows how to compare the objects contained in the list, your code is a high level level that calls a lower level (a standard java runtime list object), providing it with an interface to an object that is at your (high level) level. Then the list will "redirect" your object to complete the part of the task that does not know how to do this, namely, compare the elements of the list. Thus, in this case, the comparator can be considered as a callback object.

+8
Jan 05 2018-12-01T00:
source share

In Java, callback methods are mainly used to access the Pattern watcher , which is closely related to Asynchronous Programming.

Although callbacks are also used to simulate passing methods as a parameter, for example, what is done in functional programming languages.

+5
Jan 05 2018-12-01T00:
source share



All Articles