jshell for interactive Java programming

Java REPL, also known as jshell, is a fresh tool that was introduced in java 9. In this discussion, we will explore the fundamental principles of Java REPL and execute a few test programs within the jshell interface.

A Java Read-Eval-Print Loop (REPL)

Why was REPL support added to Java in such a late release? Let’s try to understand the significance of this decision. Scala has gained popularity in developing applications of all sizes due to its advantageous features. It supports both Object-Oriented and Functional Programming, along with the REPL feature. Oracle Corporation aims to incorporate most of Scala’s features into Java. They have already integrated some functional programming features in Java 8, such as lambda expressions. One of Scala’s standout features is the REPL (Read-Evaluate-Print-Loop), a command line interface and Scala Interpreter for executing Scala programs. Scala REPL is user-friendly and can be used to learn the basics of Scala programming and run small test code easily. Recognizing the benefits of Scala REPL in reducing the learning curve and facilitating test code execution, Java REPL was introduced in Java 9.

The Java Read-Eval-Print Loop, known as jshell.

java repl, jshell

Fundamentals of Java REPL – Introduction to jshell

java repl
scdev:~ scdev$ jshell 
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro

jshell> 

jshell> System.out.println("Hello World");
Hello World

jshell> String str = "Hello SC Users"
str ==> "Hello SC Users"

jshell> str
str ==> "Hello SC Users"

jshell> System.out.println(str)
Hello SC Users

jshell> int counter = 0
counter ==> 0

jshell> counter++
$6 ==> 0

jshell> counter
counter ==> 1

jshell> counter+5
$8 ==> 6

jshell> counter
counter ==> 1

jshell> counter=counter+5
counter ==> 6

jshell> counter
counter ==> 6

jshell> 
java repl example

Execute class in the Java REPL

It is also possible to define and execute class methods within the Java REPL shell.

jshell> class Hello {
   ...> public static void sayHello() {
   ...> System.out.print("Hello");
   ...> }
   ...> }
|  created class Hello

jshell> Hello.sayHello()
Hello
jshell> 
java repl class

Java REPL: Assistance and Termination

To access the help section of the jshell tool, simply utilize the /help command. If you wish to exit from jshell, you can do so by employing the /exit command.

jshell> /help
|  Type a Java language expression, statement, or declaration.
|  Or type one of the following commands:
|  /list [<name or id>|-all|-start]
|  	list the source you have typed
|  /edit <name or id>
...

jshell> /exit
|  Goodbye
scdev:~ scdev$ 

To exit from the jshell tool, we can utilize the Ctrl + D command. This covers the fundamentals of Java REPL and the jshell tool. For further information, visit the jshell – java shell webpage. Reference: JEP 222.

 

 

more tutorials

Addition Assignment Operator mean in Java(Opens in a new browser tab)

JSch library to execute shell commands on a Unix(Opens in a new browser tab)

breakpoint function in Python(Opens in a new browser tab)

multithreading in Java that you need to know(Opens in a new browser tab)

convert string to character array in Java.(Opens in a new browser tab)

 

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds