Master Advanced JShell: Variable Re‑declaration, Forward References & More
This article explores advanced JShell features—including variable re‑declaration, temporary variables, forward references, exception handling, session persistence, external library usage, command shortcuts, code completion, editing constructs, and programmatic API access—providing practical examples and tips for rapid Java development within the REPL environment.
JShell, introduced in JDK 9 as part of JEP 222, provides a REPL for Java, filling the gap of command‑line tools that other languages enjoy.
After covering the basics, this guide delves into several advanced concepts that speed up development.
1. Variable re‑declaration
Unlike standard Java, JShell lets you redeclare both primitive and reference variables as many times as needed.
jshell> String str="Hello"
str ==> "JShell"
jshell> Integer str=10
str ==> 102. Temporary variables
Expressions that are not assigned to a user‑named variable become temporary variables, e.g.
jshell> "Hello"+"JShell"
$1 ==> "HelloJShell"Set the feedback mode to /set feedback verbose to see variable types and evaluation details, and revert with /set feedback normal.
3. Forward references
JShell allows a method to call another method that has not yet been declared. The call succeeds only after the referenced method is defined.
jshell> public void greet(){
...> greetHelloWorld();
| created method greet(), however, it cannot be invoked until method greetHelloWorld() is declared
jshell> greet()
| attempted to call method greet() which cannot be invoked until method greetHelloWorld() is declared
jshell> public void greetHelloWorld(){
...> System.out.println("Hello World");
| created method greetHelloWorld()
jshell> greet()
Hello World4. Exception handling
JShell automatically reports uncaught exceptions. For example:
jshell> int divide(int a,int b) throws IOException{
...> if(b==0){
...> throw new IOException();
...> }
...> return a/b;
| created method divide(int,int)
jshell> divide(1,0)
| java.io.IOException thrown:
| at divide (#2:3)
| at (#3:1)Default imports can be listed with /imports, which includes common packages such as java.io.*, java.util.*, etc.
5. Session persistence
Commands entered in a session are lost on exit, but you can save a session to a file and reload it later:
jshell> String s="Hello"
s ==> "Hello"
jshell> int i=100;
i ==> 100
jshell> /save C:\data\mySession.jsh
jshell> /exit
| Goodbye
jshell> /open C:\Data\mySession.jsh
jshell> /vars
| String s = "Hello"
| int i = 1006. Using external libraries
Third‑party JARs can be added to the class‑path with the /env --class-path command, after which their classes can be imported and used.
jshell> /env --class-path ../lib/commons-lang3-3.8.1.jar
| Setting new options and restoring state.
jshell> import org.apache.commons.lang3.StringUtils;
jshell> System.out.println(StringUtils.isEmpty(""))
true
jshell> System.out.println(StringUtils.isEmpty("hello"))
false7. Handy JShell commands
Common shortcuts include /history, /list, /reset, /vars, /imports, /help, and navigation keys such as CTRL+R, CTRL+S, CTRL+C, /exit.
8. Code completion
Pressing the Tab key offers automatic completion and, with a second press, shows documentation for the selected symbol.
9. Editing constructs
The /edit command opens an editor for all constructs, or you can target a specific one, e.g., /edit 1 or /edit Person.
10. Programmatic access
Java provides an API to drive JShell programmatically, allowing REPL‑style interaction from regular Java code; refer to the official JDK documentation for details.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
