Read-Eval-Print Loop (REPL) in Java
The Read-Eval-Print Loop or REPL is a shell interface. This interface reads and evaluates each line of input and then prints the result. The Read-Eval-Print Loop helps us to interact with our application runtime present in a specific state. The commands are read and evaluated by the REPL and print the result. After printing the result, REPL goes back to the start to read, evaluate and print our next input.
Using REPL, we can easily write and test our Java code without compiling it and can see the output directly on the console.
Java doesn't have REPL already?
Definitely, a REPL must be in an established language like Java. But, not all languages have the REPL, and Java is one of them. The Java developers have required it most. Java has had something like REPL for a while as Java Beanshell. But the project was not a fully-featured REPL with other languages. For this, in 2016, Java 9 was released, which deliver a full function REPL environment.
Why is REPL so helpful?
By using the REPL, we don't need to compile or test our Java code using the javac command. After using the REPL,
We can evaluate methods, classes, and statements without creating a class. The "Hello World" program can also be written without creating a class.
Requirements for using REPL
There is only one requirement to use REPL,i.e., we should have Java 9 or higher version in our system. If Java 9 is installed in our system, we are ready to use REPL. In order to check the current version of Java in your system, open the command prompt and type the following command:
Below are some examples of REPL in which we evaluate the mathematical expressions, calculate the Fibonacci series, create a dynamic class, know the history, and modify the class.
Example 1: Evaluating Mathematical Expressions
In the first line, we create a variable "a" of type double and set its initial value 10. After that, we find the square of variable "a" and store it in the same variable. After that, we simply add 20 to the variable "a". The Jshell will put the result into a temporary variable "$3". Next, we execute the "/vars" command, which shows us all the created variables. At last, we create a variable "data" of type double and store the 5th power of digit 8.3.
Example 2: Calculating Fibonacci Series
In the first six lines of code, we create a method for the Fibonacci series. After that, we use the /methods command of Jshell, which shows us all the available methods. In the next two lines, we test the fibo() method by passing integer values. We create an array arr to determine how many terms we want to get the Fibonacci series. Next, we iterate each value of arr using for each loop. We pass each value of the arr to the fibo() method and print its return value.
Example 3: REPL for re-use
In the above code, we create a fibo() method with same return type and argument which we have created before. At this time, the Jshell overrides the previous "fibo()" method with the current one. Next, we pass each value of the arr to the function to ensure whether our fibo() method is override or not.
Example 4: Defining the class
In the above code, we create class "Student", which have Name, age, course and semester. We create a constructor in which we set the values to these variables. After the constructor, we create a method that shows the value of all the variables for each instance of the class.
Example 5: Creating an instance of the class
In the above code, we create two instances of the class and pass the value to the constructor for all the class variables. Next, we run the Jshell command to check whether s1 and s2 variables are created or not. At last, we call the showData() method to display data of each instance.