Sub topic 16
Differences from Compiled Java and When to Use JShell
This chapter assumes that you know basic Java syntax, including variables, methods, classes, and the main method. You also need a JDK 9 or later and a terminal with jshell available.
JShell is Java’s read-eval-print loop (REPL). It reads one piece of Java code, compiles it, runs it when appropriate, and prints feedback. A compiled Java program normally follows a different path: you write source files, compile them, start a class, and then run the program.
The working model
JShell divides input into snippets. A snippet is an expression, statement, declaration, import, method, or class that JShell can process as one unit.
In JShell, declarations do not need to be inside a class or method:
jshell> int limit = 3
limit ==> 3
jshell> for (int i = 0; i < limit; i++) {
...> System.out.println(i);
...> }
0
1
2The same loop can appear in a compiled program, but it must be inside a method, usually main. JShell therefore removes setup code that is useful in a complete application but distracting during experimentation.
JShell also keeps variables and methods in session state. Later snippets can use earlier declarations. Compiled Java usually gets its state from object fields, local variables, files, databases, or other services.
Choosing between JShell and compiled Java
| Use JShell when | Use compiled Java when |
|---|---|
| You want to test an expression or library call | You are building a reusable application |
| You are exploring an unfamiliar API | You need a stable project structure |
| You want immediate feedback from small changes | You need automated tests and repeatable builds |
| You are checking types, conversions, or regular expressions | You need packaging, deployment, or performance measurement |
JShell is excellent for discovery, but it is not a replacement for a build system. A session can contain old variables, modified declarations, and temporary experiments that are difficult for another person to reproduce.
Important differences and common errors
A JShell success message proves that a snippet worked in that session. It does not prove that the same text is valid as a complete .java file. For example, a top-level method works in JShell:
jshell> int cube(int value) { return value * value * value; }
| created method cube(int)In a source file, that method must be inside a class.
JShell’s automatic variables, such as $1, are also session-specific. They are convenient for exploration but should not appear in production code. A compiled program must give important values meaningful names.
Session state can create misleading results. A variable or class from an earlier experiment may still exist even after you forget how it was created. Start a new session when you need a clean test, or use /reset to remove the current state.
A practical rule
Use JShell to answer a small question quickly. Move the code into a source file when the behavior needs to be saved, tested, reviewed, packaged, or run again by someone else.
Exercise
Why might an expression work in JShell but fail when pasted into a Java source file?
Show answer
JShell accepts top-level snippets and supplies an interactive context. A Java source file requires declarations to follow the language’s file structure, such as placing a method inside a class and starting execution from a method such as main.