How JShell tool works internally in Java 9?


JShell tool has introduced in Java 9 that provides a fast and friendly environment that enables us to quickly explore, discover, and experiment with Java language features and extensive libraries.

When the code entered into the JShell console, it is processed by JLine. It is a Java library that allows us to capture on a console. Once the code has been entered, it is parsed by JShell parser in order to determine its type (method, variable, etc.).

JShell Parser is wrapped in a class with the following rules:

  • All imports are placed at the top of this class.
  • Variables, methods and class declarations become static members of this class.
  • Expressions and declarations are wrapped in a method inside this class.

After this step, the generated source code is analyzed and compiled in Bytecode by the Java compiler, then sent to a process running JVM in order to load and execute the code.

In the below code snippet, we can able to launch the JShell tool by simply typing "jshell" in command-line prompt.

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

When JShell starts, we have a command prompt waiting for input. There are two types of commands that can be entered: JShell internal commands begin with a “/” and Java code.

In the below code snippet, we can able to print both internal commands like "/list" and Java code like System.out.println().

jshell> System.out.println("Tutorialspoint")
Tutorialspoint

jshell> /list
   1 : System.out.println("Tutorialspoint")

raja
raja

e

Updated on: 10-Apr-2020

216 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements