JShell stands for java shell. It is also known as REPL (Read Evaluate Print Loop). The purpose of this tool is to provide a easy way to learn Java, but how? Lets look into it. We are aware that we have to write several lines of code to print something on screen, for example – To print “Hello World” on the screen we have to write the following code.
public class HelloWorld { public static void main(String[] args) { // Prints "Hello, World!" on the screen System.out.println("Hello, World!"); } }
This code is not easy to grasp if you are a java beginner. To learn java in a fun and interactive way, Oracle corp has come up with this tool called jshell. Such tools are already available in other popular programming languages such as Python, Scala etc.
To display the “Hello World” in JShell, all you have to write is this:
jshell> System.out.println("Hello, World!") Hello, World!
Why use JShell?
The main advantage of using jshell is that you can test your partial code (individual statements, methods etc.) here without writing the complete program and then check the various possible scenarios. Once you are satisfied with the code then you can copy it from jshell to your main program.
This allows you to try multiple scenarios without breaking your main program and gives you an opportunity to try and learn.
How to Start JShell?
To start jshell, you must have java 9 installed on your system, if you have older version of java then you can upgrade it by downloading the latest version from here.
To start the jshell, open command prompt (open terminal if you are on Mac OS X), type jshell
and hit Enter.
If you get the error like: jshell: Command Not found (as shown in the screenshot below) then you have to set the path on your System.
If everything is setup fine then you should see a screen like this.
How to exit JShell?
To stop JShell, type /exit
and hit Enter.
More Tutorials on JShell
1. JShell – working with variables
2. JShell – working with methods
Leave a Reply