Experiment with JShell
Explore expressions, variables, methods, diagnostics, and saved snippets with the real JDK REPL.
This is a fresh Java VM session. All starting files are supplied here; no notebook or earlier lesson is required. Work through the experiments, predict each result, and inspect what actually changed. Resetting or leaving discards guest files.
JShell is a tool with its own rules
JShell is the JDK's read-eval-print loop. It accepts snippets, evaluates them, and provides feedback. The lesson runs it with jshell --execution local --feedback concise -, passing snippets through a printf pipe. The guest uses an interpreted Java 27 build, so JShell needs no interpreter option. The lab uses the local execution engine so snippets run inside that same process; it avoids opening a separate debugger connection in this browser VM.
The final - tells JShell to read a script from standard input. This avoids a separate emulator fault in its interactive console. Script mode suppresses automatic expression feedback, so the commands use System.out.println(...) to show calculation results. Run each supplied printf pipeline, or edit its snippets to repeat an experiment. Finish with /exit and wait for the bash prompt before pressing Check. Each step opens the real JShell executable in its own session; saved scripts provide continuity between later sessions.
Expressions and declarations
An expression such as 6 * 7 computes a value. JShell may assign a temporary name such as $1 to a result. A variable declaration such as int hikers = 6; introduces a name and type. A println statement prints text and has no value to store as a temporary variable. Do not confuse feedback about a declaration with output printed by your program.
Inspect state with /vars. Change hikers to 9 and inspect it again. /list shows accepted snippets; it is not a listing of every character you have ever typed. Commands beginning with / are JShell commands, not Java statements.
A condition is evaluated now
Declare price 7 and print a message only when hikers is positive. A condition selects an action when the snippet runs. Reassigning hikers later does not rerun an earlier if statement automatically; submit the conditional again to observe the changed input. Use the zero case to see when nothing prints.
A small reusable method
int total(int people, int price) { return people * price; }
The first int is the return type. people and price are parameters available inside the method. return sends the calculated value to the caller. Call total(6, 7) and total(0, 7), then inspect /methods. JShell allows a top-level method declaration without writing a surrounding public class. A conventional .java application still needs its source structure.
Read diagnostics without losing accepted state
The intentionally misspelled System.out.prinln is rejected. Use the diagnostic to find the unknown method name, then submit println. A rejected snippet does not make every accepted declaration disappear. Return to /vars if you need to see what remains.
JShell accepts redefinition workflows that differ from ordinary source compilation. The browser notebook also wraps snippets in generated classes and carries successful declarations forward. Neither tool's interactive behavior should be treated as the Java language specification. Use complete source files and javac when the question is about ordinary compilation.
Save source, not a memory snapshot
/save trip.jsh writes accepted snippet source to a file. It does not serialize the running heap. /reset clears the current snippet state; /vars and /methods are then empty of your declarations. /open trip.jsh reads source and evaluates it again, rebuilding the declarations and running its statements.
After reopening, call total with a new input. The result should follow the method rule, not a saved output. Inspect trip.jsh from bash to see the difference between saved source and a transcript containing temporary results.
Transfer to a new session
The final experiment opens the saved file in a new JShell process and computes a different trip cost. This proves that the file, rather than an accidentally still-running session, carries the reusable code. No earlier notebook export is needed.
Run /help for the available command list and /help /save for save options. /exit returns to bash. If an experiment gets stuck, Reset lab discards the guest overlay and restores the supplied starting files.
Reference: Java 27 JShell guide and JShell command reference.
REPL
Read, evaluate, and print feedback for one snippet at a time.
Snippet state
Accepted variables and methods live in the current JShell session.
Script
Saved source that recreates declarations when opened; not a heap snapshot.
Discover JShell commands
Start a session, run /help, and calculate 6 * 7. Find /list, /vars, /methods, /reset, /save, /open, and /exit in the help. Exit to bash before checking.
printf '%s\n' /help 'System.out.println(6 * 7);' /exit | jshell --execution local --feedback concise -Help plus 42
Declare, reassign, and inspect variables
Declare hikers and price, calculate their product, reassign hikers to 9, and inspect /vars and /list. Identify feedback versus source text. Save the accepted source as values.jsh before exiting.
printf '%s\n' 'int hikers = 6;' 'int price = 7;' 'System.out.println(hikers * price);' 'hikers = 9;' /vars /list '/save values.jsh' /exit | jshell --execution local --feedback concise -hikers = 9; price = 7
Experiment with a boundary condition
Declare hikers as 6, run a positive-count conditional, set hikers to zero, and run the condition again. Only the first conditional should print Booking accepted. Save conditions.jsh and exit.
printf '%s\n' 'int hikers = 6;' 'if (hikers > 0) System.out.println("Booking accepted");' 'hikers = 0;' 'if (hikers > 0) System.out.println("Booking accepted");' '/save conditions.jsh' /exit | jshell --execution local --feedback concise -Booking accepted once
Declare and call a method
Open the supplied start.jsh, inspect /methods, and call total for 6 and 0 people. Identify the return type, parameters, and multiplication rule. Save trip.jsh before exiting.
printf '%s\n' '/open start.jsh' /methods 'System.out.println(total(6, 7));' 'System.out.println(total(0, 7));' '/save trip.jsh' /exit | jshell --execution local --feedback concise -Method listed; results 42 and 0
Repair a rejected snippet
Open trip.jsh, submit the misspelled prinln call, then submit println correctly. The unknown-method diagnostic is expected. Inspect /vars to see that accepted variables still exist; exit to bash.
printf '%s\n' '/open trip.jsh' 'System.out.prinln("Trip ready");' 'System.out.println("Trip ready");' /vars /exit | jshell --execution local --feedback concise -Unknown method diagnostic, then Trip ready
Reset and reopen saved source
Open trip.jsh, reset, and inspect /vars and /methods. Reopen the saved source and calculate total(3, 7). The declarations return because source is evaluated again, not because reset failed.
printf '%s\n' '/open trip.jsh' /reset /vars /methods '/open trip.jsh' 'System.out.println(total(3, 7));' /exit | jshell --execution local --feedback concise -After reopen: 21
Inspect the saved file and transfer to a new session
Read trip.jsh in bash. Then start a fresh JShell session, open the file, and call total(4, 8). Explain why a source script can recreate declarations but does not preserve arbitrary runtime objects.
cat trip.jshprintf '%s\n' '/open trip.jsh' 'System.out.println(total(4, 8));' /exit | jshell --execution local --feedback concise -Fresh process: 32
You completed the tooling workflow using real commands and files. Explain each result before moving on; tool output is evidence, not a substitute for the Java rule behind it.
Enable JavaScript to run the live terminal and track your progress.