璀璨人生叶辰萧初然免费阅读无弹窗全文阅读_璀璨人生叶辰 璀璨人生叶辰萧初然免费阅读无弹窗全文阅读_璀璨人生叶辰 ,女王狗奴最新章节目录_女王狗奴最新章节 女王狗奴最新章节目录_女王狗奴最新章节

发布日期:2021年05月18日
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.

Read-Eval-Print Loop (REPL) in Java

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,

  1. No need of an editor to write the Java program.
  2. No need to save the Java program.
  3. No need to compile Java program.
  4. No need to do editing if any compile-time or runtime error comes.
  5. No need to repeat the process.
Read-Eval-Print Loop (REPL) in Java

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:


Read-Eval-Print Loop (REPL) in Java

Using REPL

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

Description:

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.

Output:

Read-Eval-Print Loop (REPL) in Java

Example 2: Calculating Fibonacci Series

Description:

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.

Output:

Read-Eval-Print Loop (REPL) in Java

Example 3: REPL for re-use

Description:

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.

Output:

Read-Eval-Print Loop (REPL) in Java

Example 4: Defining the class

Description:

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.

Output:

Read-Eval-Print Loop (REPL) in Java

Example 5: Creating an instance of the class

Description:

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.

Output:

Read-Eval-Print Loop (REPL) in Java




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA

璀璨人生叶辰萧初然免费阅读无弹窗全文阅读_璀璨人生叶辰 璀璨人生叶辰萧初然免费阅读无弹窗全文阅读_璀璨人生叶辰 ,女王狗奴最新章节目录_女王狗奴最新章节 女王狗奴最新章节目录_女王狗奴最新章节