You are here
Home > java > Core Java >

Continuation-Passing Style-CPS in Java

Continuation-Passing Style-CPS in JavaContinuation-Passing Style (CPS) is a programming model used to manage control flow in software applications. It provides a way to handle asynchronous or callback-based programming by explicitly passing control from one function to another. Although this concept was originally generalized in functional programming languages like Scheme, Haskell, and Scala, but CPS can also be implemented in object-oriented languages like Java. In this article, we’ll analyze the concepts behind Continuation-Passing Style-CPS in Java and explore its implementation with code examples.

What is Continuation-Passing Style?

In traditional programming model, control flow is managed through the call stack. When a function is called, the current execution context is pushed onto the stack, and when the function returns, the context is popped off the stack, allowing the program to resume execution from the caller’s context.

CPS, on the other hand, abstracts control flow by passing around “continuations,” which are essentially functions representing the remaining computation to be performed. Instead of returning values directly, functions in CPS take an additional argument representing the continuation to be invoked upon completion. This enables more flexibility in managing control flow, particularly in scenarios involving asynchronous operations or complex branching logic.

Normally a function takes some input, returns some value, and then the caller of the function continues to do something with that returned value. In Continuation-Passing Style (CPS), rather than a function directly giving back a result, it takes another function called a continuation as its parameter. This continuation describes what should be accomplished with the development of the function. So, rather than just replacing a value, the function calls this continuation, passing the result to it.

What are the benefits of Learning Continuation-Passing Style-CPS in Java?

Even though Java doesn’t natively support CPS, understanding the concept can be beneficial for several reasons:

  • Advanced Control Flow: CPS allows for expressing complex control flow patterns in a more explicit way. This can be helpful for understanding concepts like continuations and non-local control flow.
  • Functional Programming: CPS shares similarities with functional programming paradigms. Learning CPS can provide a connection to understand functional languages where CPS is more commonly used.
  • Problem-Solving Approach: CPS encourages thinking about problems in terms of passing continuations, which can lead to more refined solutions in specific scenarios.

Continuation-Passing Style-CPS in Java

As per the Oracle’s official documentation, Continuation interface is used to implement asynchronous post-processing, the pattern that is also known as the “Continuation-passing style”. Most commonly, a continuation can be used to encode a single program control mechanism (i.e. a logical “return”). Oracle documentation defines the Continuation Interface under package “” as below:

public interface Continuation<R>

where R is the result type.

It has a void method:

void proceed(R r)
It resumes the execution after the completion of an asynchronous call.
r is the result of the execution preceding this continuation

Advanced usages may also need to encode multiple control mechanisms (e.g. an exceptional execution path). For such usages, each control path could be explicitly represented as a separate Continuation, e.g.:

 void doAsync(Continuation<Result> contNormal, Continuation<Exception> contExceptional);

All Known Implementing Classes:

AbstractPersistenceEnvironment.DefaultFailureContinuation, NullImplementation.NullContinuation, NullImplementation.NullPersistentStore.Token

At first look, the Continuation-Passing Style (CPS) may appear unorganized and not very reasonable. After all, it assembles function types longer, codes harder to comprehend, and doesn’t appear to offer much that regular functions can’t handle. However, CPS has some key qualities that make it valuable:

Key Features of CPS 

Clearer Control Flow: CPS makes the flow of your program clearer by explicitly showing where it’s going next. This enables avoid confusion, especially in the case of complex programs.

Great for Asynchronous Tasks: It should be known that it’s really useful for dealing with tasks that don’t happen one after the other, but rather than happen independently or at different times. CPS makes it easier to manage these asynchronous events without creating messy code.

Easier Error Handling: CPS facilitates developers to find error handling by using errors to be managed centrally, rather than scattered throughout the code. This makes it easier to spot and fix problems within seconds.

Optimization Potential: CPS can periodically be optimized by compilers that make your code run faster and more efficiently. It is especially for certain types of tasks like recursive functions in the code.

Works Well with Other Systems: We should keep in mind that it’s also convenient when we need our code to work smoothly with other systems or languages. Therefore, we can say that CPS is useful naturally.

Tail Call: It is important to know that every function call is a tail call. In Continuation-Passing Style (CPS), every function call is like the last action in a sequence, or what’s called a “tail call.” We never just call a function and move on; rather, we provide it a job to do after it’s accomplished, called a “continuation”. Remember that functions don’t actually “return” results in the usual way since there are different ways as explained in above; they pass them directly to these continuations. Therefore, in CPS, there’s no such thing as a regular function call where we use the returned value right away and everything is set up to keep the sequence going smoothly.

Important Concepts on CPS

In Continuation-Passing Style (CPS), there are several important concepts that become more obvious.

  • Let’s assume about what happens when we call a function. Usually, we don’t worry about where the function should “return” to once it’s done since that’s handled behind the scenes during the development. But in CPS, this return address is straightforward: it’s the continuance. Therefore, when a function finishes, it just calls its continuation to pass back the result.
  • Also, think about the call stack, the record of all the functions that have been called. In CPS, the continuations can be seen as expressing this call stack. Each time a function is called, a new continuation is created, kind of like adding a new layer to the stack.
  • CPS also creates it more apparent what occurs in what order and what middle results are called. For example, in a regular function, it’s not always apparent which part of a measure happens first. But in CPS, everything is spelled out explicitly. We can see precisely which steps ensue when, and what each step is called in the function. This clarity can make code easier to understand and debug during the development.
  • Having continuations as first-class functions extends a world of opportunities in development. When we are writing a regular function during the code, we are limited to just returning the result at the end of the function. But with continuations, we have more flexibility and power. Therefore, it is best in this regard. For example, we can do things like calling the continuation of an enclosing function – effectively, jumping back to where we came from and resuming from there. This can be helpful in certain situations, such as handling errors or implementing complex control flow in the development.
  • In addition to that, we can also give continuations to other functions, simply like we would with any other parameter of the function. In other words, we can say that we can easily compose functions in interesting ways, chaining them together to perform more complex behavior
  • And ultimately, we can store continuations in data structures and use them later. This opens up all sorts of opportunities for things such as building custom control flow constructs or executing advanced algorithms in development.

Therefore, by treating continuations as first-class functions, we accumulate a lot more flexibility and power in how we structure and use our code.

Key Concepts of CPS


As mentioned earlier, continuations represent the remaining computation to be performed after a particular function finishes executing. In CPS, functions take continuations as arguments and invoke them to pass control to the next stage of computation.

Asynchronous Programming

CPS is particularly useful in asynchronous programming scenarios, where operations may take an unknown amount of time to complete. By explicitly passing continuations, CPS enables developers to handle asynchronous callbacks in a structured and composable manner.

Tail Calls

In CPS, tail calls are a crucial optimization technique. A tail call occurs when a function’s last action is to call another function. By optimizing tail calls, CPS can avoid stack overflow issues that may arise from deeply nested function calls.

Usages of CPS

  • Asynchronous Programming: CPS facilitates asynchronous programming, making it easier to manage concurrent tasks. 
  • Modularization: CPS encourages modularization of code by separating concerns into smaller, composable functions.
  • Error Handling: CPS simplifies error handling in asynchronous code by propagating errors through continuations.
  • Debugging: CPS can make debugging easier by providing clear separation between different stages of computation.

Implementing CPS in Java

Java itself doesn’t directly support continuation-passing style (CPS) like some other functional languages. However, we can achieve a similar effect using functional interfaces and anonymous functions.

Here’s the idea behind CPS:

  • A function receives an extra argument called a “continuation.” This continuation is another function that specifies what to do with the result of the current function.
  • Instead of directly returning a value, the function calls the continuation and passes the result as an argument.


Here’s an example in Java to illustrate this concept. This example demonstrates how we can achieve a CPS-like behavior using functional interfaces and anonymous functions in Java. The flow of control is passed explicitly through the continuation, and functions don’t directly return values but instead call the continuation with the result.

// A functional interface representing a continuation

public interface Continuation<R> {
   void apply(R result);

public static void factorialCPS(int n, Continuation<Integer> cont) {
   if (n == 0) {
   } else {
     factorialCPS(n - 1, result -> cont.apply(result * n));

public static void main(String[] args) {
   factorialCPS(5, result -> System.out.println("Factorial of 5: " + result));
  • We define a Continuation interface that takes one argument of type R. This represents the function that will handle the result.

  • The factorialCPS function takes an integer n and a Continuation as arguments.

  • If n is 0, the factorial is 1. We directly call the apply method of the continuation with the result (1).

  • If n is greater than 0, we need to calculate the factorial of n-1 first. We make a recursive call to factorialCPS with n-1.

  • Inside the recursive call, we provide an anonymous function as the continuation. This function receives the result of the recursive call (factorial of n-1) and multiplies it by n to get the final factorial. It then calls the original continuation with this final result.

  • In the main method, we call factorialCPS with 5 and an anonymous continuation that simply prints the result.


Now let’s see another example to implement CPS in Java. We will define a simple asynchronous operation using CPS.

import java.util.function.Consumer;

public class CPSExample {
   public static void main(String[] args) {

      // Simulate an asynchronous operation that takes some time to complete

       asynchronousOperation("Hello", result -> {

       System.out.println("Result: " + result);



 public static void asynchronousOperation(String input, Consumer<String> continuation) {

      // Simulate asynchronous behavior with a delay

     new Thread(() -> {

     // Perform some computation

     String result = input + " World!";

     // Invoke the continuation with the result




In this example, the asynchronousOperation method takes an input string and a continuation as arguments. Inside the method, we simulate an asynchronous operation by replicating a new thread and performing some computation. Once the computation is complete, we invoke the continuation with the result.

Example#3: Handling Multiple Asynchronous Operations

CPS becomes even more powerful when dealing with multiple asynchronous operations. Let’s see how we can chain together multiple asynchronous calls using CPS.

public class CPSExample {

   public static void main(String[] args) {

     // Chain together multiple asynchronous operations

       operation1("Hello", result1 -> {

       operation2(result1, result2 -> {

       System.out.println("Final result: " + result2);




   public static void operation1(String input, Consumer<String> continuation) {

        // Simulate asynchronous behavior with a delay

        new Thread(() -> {

        String result = input + " World!";



   public static void operation2(String input, Consumer<String> continuation) {

        // Simulate asynchronous behavior with a delay

       new Thread(() -> {

       String result = input.toUpperCase();



In this example, we chain together two asynchronous operations (operation1 and operation2) by passing continuations to each operation. This allows us to compose asynchronous workflows in a sequential manner, making the code easier to read.

Example: Optimizing with Tail Calls

As mentioned earlier, optimizing tail calls is crucial in CPS to avoid stack overflow issues. Here’s an example of optimizing a factorial function with tail calls in CPS (Continuation Passing Style) for Java:

Normal Factorial (with Stack Overflow Risk):

public static int factorial(int n) {
   if (n == 0) {
     return 1;
   } else {
     return n * factorial(n - 1);

This standard factorial function is recursive, but it’s not tail-recursive. Each recursive call creates a new stack frame, which can lead to a stack overflow error for large values of n.

Tail-Recursive Factorial in CPS:

public static void factorialCPS(int n, IntFunction<Void> continuation) {
    if (n == 0) {
      continuation.apply(1); // Base case, apply continuation with 1
    } else {
     // Tail-recursive call with a new continuation
      factorialCPS(n - 1, result -> continuation.apply(n * result));

This version uses CPS. It takes an IntFunction<Void> continuation as an argument. Inside the function:

  • Base case (n == 0): Directly applies the continuation with the value 1.
  • Recursive case: Performs a tail-recursive call with n-1. However, instead of directly returning a value, it creates a new continuation that multiplies the result with n and then calls the outer continuation with the final result.

Note: Java doesn’t have guaranteed tail call optimization (TCO). While the code is written for tail recursion, the JVM might not optimize it completely. This example showcases the concept, but for production use, consider other iterative or memoization approaches for factorial calculation to avoid potential stack overflow issues.

Challenges and Considerations

  • Complexity: CPS can introduce additional complexity, especially in large codebases. 
  • Callback Hell: Improper use of CPS can lead to callback hell, where code becomes hard to read and maintain.
  • Performance Overhead: There might be a slight performance overhead associated with CPS due to the additional function calls and context switching.


Developers can leverage this CPS model to write more efficient and maintainable asynchronous code in Java by understanding the key concepts of it and exploring practical examples, In summary, CPS offers a flexible and powerful approach to managing control flow in asynchronous programming, making it a valuable tool for modern software development.

Learning CPS can extend our problem-solving skills and strengthen our understanding of functional concepts, even if we don’t directly use it in everyday Java development. For practical use of CPS, languages like Scheme, Haskell, and Scala are more suited due to their built-in support and functional nature.

This concludes our exploration of Continuation-Passing Style (CPS) in Java. I hope you found this article informative and helpful in understanding this important programming model.

FAQs on Continuation-Passing Style (CPS) in Java

What is Continuation-Passing Style (CPS) in Java?

In traditional programming, functions return values. In CPS, functions don’t directly return values. Instead, they take an additional argument called a continuation, which is another function. The original function then calls the continuation, passing the result (or error) as an argument. This style forces all control flow to be explicit through continuations.

Why to use CPS in Java?

While not as common as traditional styles in Java, CPS offers some benefits:

  • Clearer Control Flow: CPS makes the flow of our program more explicit by showing where execution continues after a function call. This can be helpful for complex asynchronous tasks.
  • Easier Error Handling: Continuations can handle errors explicitly, potentially simplifying error management.
  • Tail-call Optimization: CPS can be beneficial for languages with tail-call optimization, as CPS functions often end in tail calls to continuations.

How can we implement CPS in Java?

Java doesn’t have built-in support for CPS. However, we can achieve a similar effect using interfaces or anonymous functions:

interface Continuation<T> {
  void proceed(T result);

void someFunction(int x, Continuation<Integer> cont) {
  int result = x * 2;
  cont.proceed(result); // Pass the result to the continuation

Is CPS commonly used in modern Java development?

CPS is not a mainstream approach in Java development. Libraries like RxJava or Project Loom offer alternative ways to handle asynchronous tasks and control flow. However, understanding CPS can be beneficial for advanced functional programming concepts and potential future language features.

Leave a Reply