Sobes.tech
Junior — Middle

What is the purpose of the Observable interface in reactive programming?

sobes.tech AI

Answer from AI

The Observable interface in reactive programming represents a data source that can asynchronously emit a sequence of events (data) to subscribers (Observers). Its main purpose is to provide a data stream that can be subscribed to and receive updates in real-time, simplifying working with asynchronous operations and events.

Example of using Observable in RxJava:

Observable<String> observable = Observable.just("Hello", "World");
observable.subscribe(
    item -> System.out.println("Received: " + item),
    error -> System.err.println("Error: " + error),
    () -> System.out.println("Completed")
);

Here, Observable generates a sequence of strings, and the subscriber receives them as they appear.