introduction to jshell

The Java Shell tool (JShell) is an interactive tool for learning the Java programming language and prototyping Java code. JShell is a Read-Evaluate-Print Loop (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results. The tool is run from the command line.

This is a new feature of JAVA 9 so , java 9 must be installed and path is set on your system before using jshell .


How to open jshell?

To open jshell , open your cmd and type command :

jshell
To start JShell in verbose mode, use the -v option:
jshell -v


C:\Users\Durgesh>jshell
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro

The examples in this tutorial use the verbose mode. Verbose mode is recommended as you work through this tutorial so that what you see matches the examples. When you are more familiar with the tool, you might prefer to run in normal or a more concise mode.


C:\Users\Durgesh>jshell -v
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro

jshell> int i=5;
i ==> 5
|  created variable i : int

jshell>

To exit the jshell type command :

 /exit 


jshell> /exit
|  Goodbye

C:\Users\Durgesh>

Why Use JShell?

Using JShell, you can enter program elements one at a time, immediately see the result, and make adjustments as needed.

JShell helps you try out code and easily explore options as you develop your program. You can test individual statements, try out different variations of a method, and experiment with unfamiliar APIs within the JShell session. JShell does not replace an IDE. As you develop your program, paste code into JShell to try it out, and then paste working code from JShell into your program editor or IDE.

Java program development typically involves the following process:

  Write a complete program.

  Compile it and fix any errors.

  Run the program.

  Figure out what is wrong with it.

  Edit it.

  Repeat the process.

woman avatar