Browse Clojure Foundations for Java Developers

Basic REPL Usage

Use the REPL as a real development tool: evaluate forms, inspect values, and require the right helpers instead of guessing.

For a Java developer, the REPL is the first tool that changes how you think about programming in Clojure. It is not just a console and it is not just a teaching toy. It is the fastest way to evaluate code, inspect data, and shorten the gap between an idea and a result.

The most important early shift is this:

Do not wait until the whole application is wired up before testing a small idea.

The REPL exists so you can test the small idea immediately.

Start With clj

For interactive work, start the REPL like this:

1clj

The current CLI reference uses clj for REPL examples and clojure for non-REPL commands. The practical reason is simple: clj is the friendlier interactive entry point.

When it starts, you will usually land in the user namespace:

1user=>

That prompt matters. It tells you:

  • you are in a live evaluation environment
  • your current namespace is user
  • forms you type are about to be read, evaluated, and printed back

Evaluate Forms, Not Lines Of Syntax

The REPL does not care about “lines” the way many shell users expect. It reads forms.

Start with a few simple examples:

1(+ 1 2 3)
2;; => 6
3
4(map inc [1 2 3])
5;; => (2 3 4)
6
7{:user/id 42 :user/status :active}
8;; => {:user/id 42, :user/status :active}

Use the REPL to build the habit of asking:

  • what form am I evaluating?
  • what value does it return?

That is more important than memorizing commands.

Define Small Values And Functions

The REPL is a great place to define scratch values and tiny functions while you learn or investigate behavior:

1(def tax-rate 0.13)
2
3(defn total-with-tax [subtotal]
4  (+ subtotal (* subtotal tax-rate)))
5
6(total-with-tax 100)
7;; => 113.0

This is especially useful for Java developers because it removes the compile-run-edit cycle for small experiments.

But notice the right lesson:

  • use def freely in the REPL while experimenting
  • do not treat endless REPL-only defs as a substitute for real source files

The REPL is where you discover and verify. The source file is where the lasting program goes.

Know When REPL Helper Functions Are Already Available

There is an important nuance here. In a plain clojure.main REPL started by clj, the default user namespace already has several helpful inspection functions referred for you, including:

  • doc
  • find-doc
  • source
  • apropos

So in the default user=> prompt, these will usually work immediately:

1(doc map)
2(find-doc "reduce")
3(source merge)
4(apropos "zip")

However, if you switch to a different namespace, do not assume those helpers came with you. In that case, requiring them explicitly is still the reliable move:

1(require '[clojure.repl :refer [doc find-doc source apropos]])

This is a good lesson in Clojure itself:

  • the REPL is powerful
  • but it still has namespace context
  • helper availability depends on where you are evaluating code

Use REPL Result Vars

Clojure keeps a few helpful vars around for recent REPL values:

  • *1 for the most recent result
  • *2 for the result before that
  • *3 for the one before that
  • *e for the most recent exception

Example:

1(* 6 7)
2;; => 42
3
4*1
5;; => 42

These are small conveniences, but they become useful once you are exploring or debugging data step by step.

Learn The Difference Between REPL State And Program State

This is a subtle but important shift for Java engineers.

When you define something at the REPL:

1(def config {:port 8080})

you have changed the live REPL session. You have not necessarily changed any source file. That means:

  • the value exists in the current session
  • restarting the REPL may remove it
  • the project source is unchanged unless you edit files too

This is powerful, but it also means you need discipline. Use the REPL to explore. Put the result of that exploration back into source files intentionally.

Command History Depends On The Front End

When you start the REPL with clj, you will usually have comfortable command history and line editing. That behavior often comes from the CLI wrapper and terminal setup, not from some mysterious property of the language itself.

So:

  • use arrow keys for previous forms when your terminal supports it
  • prefer clj for interactive work
  • do not assume every REPL front end behaves identically

If you are in an editor-integrated REPL later, it may feel even better. But your terminal REPL should already be enough to learn with.

Exit Cleanly

When you are done with a plain terminal REPL, use:

  • Ctrl-D

That exits the session. Learn this early so you do not end up killing terminals unnecessarily.

A Good Beginner Workflow

Once the REPL is open:

  1. evaluate a small expression
  2. define a tiny function
  3. call it with a few sample values
  4. require helper namespaces when needed
  5. move the lasting code into a source file

That is a much healthier loop than piling up ad hoc definitions for half an hour and forgetting which ones matter.

Common Mistakes

  • treating the REPL like a shell instead of a form-based evaluator
  • assuming helper functions like doc are always already loaded
  • using the REPL only for arithmetic instead of real function and data exploration
  • forgetting that REPL state is not the same thing as saved source code
  • waiting too long to move proven code back into files

The REPL is most useful when it feeds your source code, not when it replaces it.

Knowledge Check: Building A Real REPL Habit

### What is the main advantage of the REPL for a Java developer learning Clojure? - [x] It shortens the feedback loop so you can evaluate and inspect ideas immediately. - [ ] It removes the need for source files. - [ ] It replaces dependency management. - [ ] It automatically writes tests. > **Explanation:** The REPL's biggest benefit is fast feedback. You can try small pieces of code immediately instead of wrapping everything in a full program run. ### In a plain REPL, what is the reliable way to use `doc` and `source`? - [x] Require `clojure.repl` and refer the helpers you want. - [ ] They are always built-in commands with no setup. - [ ] Install Leiningen first. - [ ] Restart the JVM after every expression. > **Explanation:** In a plain REPL, helper functions such as `doc`, `find-doc`, `source`, and `apropos` come from the `clojure.repl` namespace. ### What does `*1` contain? - [x] The most recent REPL result - [ ] The first value ever evaluated in the session - [ ] The current namespace - [ ] The last command as a string > **Explanation:** `*1` holds the latest result value, which is useful when exploring incrementally. ### Why is `def` in the REPL different from editing a source file? - [x] It changes the current live session, but not necessarily the saved program on disk. - [ ] It compiles code to native binaries. - [ ] It is only temporary and can never affect runtime. - [ ] It disables the namespace system. > **Explanation:** REPL definitions affect the live environment immediately, but they do not automatically update your source files. ### What is the best default command for starting a terminal REPL? - [x] `clj` - [ ] `lein uberjar` - [ ] `java -jar` - [ ] `git repl` > **Explanation:** The current CLI reference uses `clj` for REPL examples because it is the friendlier interactive entry point.
Revised on Friday, April 24, 2026