Simple, Powerful Interactive Java, JShellNovember 3, 2022
Simple, Powerful Interactive Java, JShell
JShell is used to write your java declarations, evaluations, expressions and interactively evaluate these and see the result immediately and remember your statements for future usage in the existing running instance. This shell for Java which is a RPEL (stands for Read Evaluate Print Loop) tool was introduced with JDK 9.
How To Open JShell
I often use this handy shell to make experiments with my code or to try new things. Below screenshot is the welcome screen for jshell and you need JDK 9 or above in order to execute it. Just go to bin folder and type jshell and here you enter the world or REPL shell screen.
You can write your java code within that shell and it reads and it quickly evaluates your code and prints the result in the screen and till you exit your instance, it will remember your statements so you can loop in your pre-evaluated statements. This interactive shell is really beneficial if you are willing to experiment your code quickly and it is a handy tool for the ones who jumps into java world.
When you type “/help” in the shell command line, commands will be listed and you can easily adopt jshell with this help tutorial.
You can list all codes you can type, or you can open a file within jshell, or you can see the imports that you have done so far, or you can list the variables with that transaction easily with the help of jshell commands.
Playing With Variables
As you can see from the below screenshot, it is easy to use jshell. You can declare variables or you do not need to declare them, jshell creates temp variables if you do not declare them and infer them to the related types. For instance, when you type “i*3;” in the command line and jshell evaluates this statement and creates a temp int variable, $2 and prints it. So, you can also uses “/vars” command to see you pre-declared variables.
One more important thing is that static & final declarations are not permitted for variables.
Also, methods can be created easily within jshell and then it is just a command to execute your method. You do not need to specify access modifiers while declaring you method. You can also list your methods via “/methods” command.
Also you can import other classes as usual and use them afer your import. You can list your imports with the help of “/imports” command.
Create Classes & Play With Them
You can also code as you do all the time, create classes, arrange inheritances, create lists and play with them.
Even you can use var in jshell if you have at least JDK 10. And, jshell interactively displays the errors that you have in your statements.
Opening a Fancy JShell Editor
There is a magic hidden under “/edit” command. If you type “/edit” in the command line, a more user-friendly fancy editor (JShell Edit Pad) is opened, and then you can continue from this editor.
And if you do not want to play with shell via your terminal, you can open JShell console from one of the popular IDEs, IntelliJ IDEA from Tools -> JShell Console path.
I really like jshell as it a powerful & easy RPEL tool. I can test new statements or see how my code behaves line by line, variable by variable, method by method and so on.
I hope this article helps you understand the power of jshell and triggers you to use it even it seems it is a simple tool for most senior developers. But, pls remember that all complex architectures builds on a simple idea.