Do you know Java: JShell


JShell interpreter can process Java statements without requiring them to be placed in class and method body.

JShell interpreter and CLI were introduced in Java 9. That come handy when we would like to execute a statement, or call a subroutine to get their results.

Shell

To enter the shell, we should type jshell in the command line and hit enter.

|  Welcome to JShell -- Version 13
|  For an introduction type: /help intro

jshell>
|  Welcome to JShell -- Version 13
|  For an introduction type: /help intro

jshell> int i = 1 + 2;
i ==> 3

jshell> System.out.println(i);
3

Commands

To see what else we can do with it, just type /help as it is hinted in the header of the shell. We can check the imported types (/imports), or types (/types), methods (/methods), variables (/vars). And we can reset the shell as well (/reset).

Tab complition is also supported by jshell.

jshell> int i = 1 + 2
i ==> 3

jshell> /vars
|    int i = 3

Fortunately, no semicolon needed.

As we see, Java 9 brought a Java shell, called JShell with which we can execute statements without any boilerplate text.