What is the importance of REPL in Java 9?

JavaObject Oriented ProgrammingProgramming

REPL stands for Read-Eval-Print-Loop. It is a shell where the user types an expression, it's evaluated, and the result returned to the user. The main purpose of using REPL is to interact quickly with Java programs without creating a java file, compile it, and run it. JShell is very useful for the developers and allows us to learn the Java language.

Below are some of the features of REPL

  • It’s built-in in Java 9.
  • We can test any Java expression without a class file, compiling and running it.
  • It autocompletes the methods, just typing the TAB key, as in your editor.
  • We can define methods, and call them later.
  • It shows us the exceptions if they are thrown.
  • We can edit any method, and change them.
  • There are built-in commands.

Below are the few examples of REPL using the Jshell tool.

Example-1

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

jshell> Math.round(34.543)
$1 ==> 35


Example-2

jshell> void test(String s) {
...>       System.out.println(s);
...>    }
| created method test(String)

jshell> test("TutorialsPoint")
TutorialsPoint
raja
Published on 27-Mar-2020 21:34:10
Advertisements