Java shell prompt using JShell. 5 minutes intro
TL; DR
Type jshell
and type auto-completed java code in a prompt, immediately executed. Use $1 to access output from previous commands, type /save <file.jsh>
to save the written code into a file and jshell <file.jsh>t
to execute it.
What JShell is
JShell is a Java REPL (Read-Evaluate-Print Loop) tool that was introduced in JDK 9. It is an interactive tool for learning the Java programming language and prototyping Java code.
What it does
JShell evaluates declarations, statements, and expressions as they are entered and immediately shows the results. It provides a way to interactively evaluate Java statements, variable definitions, method definitions, class definitions, import statements, and expressions. You can use JShell to learn the Java programming language, explore unfamiliar code and APIs, and prototype complex code.
How to use it
To use JShell, you can start by opening a command prompt or terminal window and typing jshell
. Once you start JShell, you can enter Java code and see the results immediately. You can type any valid expressions and statements in the Java language, and JShell gives you results immediately. You can declare a Java method or class using JShell as well.
Example: declare two vars and sum them
You can start JShell by opening a command prompt or terminal window and typing jshell
. Once you start JShell, you can enter Java code and see the results immediately. For example, you can enter the following code:
jshell> int x = 5;
x ==> 5
jshell> int y = 10;
y ==> 10
jshell> int z = x + y;
z ==> 15
In this example, we declare two integer variables x
and y
, and then we add them together and store the result in a third variable z
. JShell immediately shows us the value of each variable as we declare it.
To reuse previous commands in JShell, you can use the up arrow key to navigate through your command history. Pressing the up arrow once replaces the current line with the previous command or snippet line. Pressing the up arrow again brings you to the line previous to that.
Access the previous variables with $1
the $1
variable refers to the value of the previous expression that was evaluated. In other words, $1
is a handy way to refer to the result of the previous expression without having to retype it. Here's an example:
- Type
2 + 3
and press Enter. JShell will evaluate the expression and print the result, which is5
. - Type
$1
and press Enter. JShell will print the value of the previous expression, which is5
.
How is it useful to you
- Test Java syntax: JShell is a great tool to experiment with Java syntax. You can try out new features and constructs without having to create a full Java program.
- Evaluate expressions: JShell lets you evaluate Java expressions and get their results immediately. This makes it easy to check the behavior of a small piece of code before integrating it into a larger project.
- Explore Java APIs: You can use JShell to explore Java APIs and learn how they work. JShell provides auto-completion, so you can easily find classes, methods, and fields.
- Debug code: You can use JShell to debug code by stepping through expressions and checking their results. This can be especially helpful when you’re trying to identify the source of a problem.
- Create scripts: JShell lets you create scripts that can be run from the command line. This can be a useful way to automate repetitive tasks or to create small utilities. Once you’ve entered all of the code that you want to include in the script, type
/save filename.jsh
and press Enter to save the code into that file. To run the script, typejshell filename.jsh
in a terminal.
How to install
If you have installed the Java Development Kit (JDK) using SDKMAN, then JShell should already be installed on your system. If you get an error saying that jshell
is not a recognized command, then it's possible that the JDK installation was not properly configured on your system. In this case, you may need to add the JDK's bin directory to your system's PATH environment variable.
More info
More information about JShell on Oracle’s website
Clap if useful.
And also say thanks to chat GPT 3 that basically wrote most of the article's boring parts following my instructions :D