Java 9 - REPL (JShell)

REPL stands for Read-Eval-Print Loop. JShell allows us to execute Java code snippets

Java REPL application name is jshell. JShell stands for Java Shell. jshell is an interactive tool to execute and evaluate java simple programs like variable declarations, statements, expressions, simple Programs etc.

Java REPL – jshell basics

        javacode:~ geeks$ jshell
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro


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

jshell> String str = "Hello Javacodegeeks Users"
str ==> "Hello JournalDev Users"

jshell> str
str ==> "Hello Javacodegeeks Users"

jshell> System.out.println(str)
Hello Javacodegeeks 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

Java REPL – execute class

We can also define and execute class methods in 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 – Help and Exit

To get jshell tool help section, use /help command. To exit from jshell, use command /exit.

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 java:~ java$

javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition