liveBook · Manning

Imperative programming

As explained in Spring in Action, Sixth Edition

Imperative programming is a programming approach in which computation is expressed as an ordered sequence of commands that directly update state or perform actions.

Imperative programming expresses computation as an ordered list of instructions. Each task runs one at a time, and the program waits for it to finish before continuing. Data must generally be available as a complete unit before the next step processes it. section 11.1

Imperative execution is straightforward, but its sequential model has practical costs:

  • A task blocks the executing thread until it completes, especially during I/O such as database writes or remote-server requests. section 11.1
  • Adding threads can allow concurrent work, but those threads may also block, making concurrency harder to manage and increasing complexity. section 11.1
  • Imperative code remains useful and widely used; reactive programming is an alternative for situations where its model fits better, not a universal replacement. section 11.0 section 11.1

Sequential greeting

A simple imperative program assigns a name, converts it to uppercase, builds a greeting, and prints the result. Each line completes before the next begins. section 11.2

String name = "Craig";
String capitalName = name.toUpperCase();
String greeting = "Hello, " + capitalName + "!";
System.out.println(greeting);

View the complete Java listing for the sequential greeting example. section 11.2

How Spring in Action, Sixth Edition covers it

How deep it goes
Defines sequential execution and blocking, then stops short of explaining imperative implementation internals.
Examples
Uses a Java greeting that uppercases a name, constructs a message, and prints it through sequential statements.
What you can do after
Evaluate/understand how sequential tasks, complete data, and blocking distinguish imperative code from reactive pipelines.
How it connects
Connects imperative programming with reactive programming, concurrency, blocking I/O, threads, and data processing.

Also explained in

Compare how 14 books explain imperative programming →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.