Using an interactive shell (REPL)

Java:
Using an interactive shell (REPL)

How to:

Starting a REPL in Java is simple with the jshell tool introduced in Java 9. Here’s how to get your hands on it and start a basic session:

jshell> int sum(int a, int b) {
   ...> return a + b;
   ...> }
|  created method sum(int,int)

jshell> sum(5, 7)
$1 ==> 12

Exit any time with /exit.

jshell> /exit
|  Goodbye

Deep Dive

Before jshell, Java programmers didn’t have an official REPL, unlike Python or Ruby devs. They used IDEs or wrote full programs even for trivial tasks. jshell was a game-changer as of Java 9, bridging that gap.

Alternatives include online compilers or IDE plugins, but they don’t match jshell’s immediacy. As for internals, jshell uses the Java Compiler API to execute code fragments, which is pretty neat. It’s more than a playground—it can import libraries, define classes, and more. This makes it a robust tool for prototyping.

See Also