logo

REPL (JShell)


Show

Read-Eval-Print-Loop is the full form of REPL. With JShell, java has REPL capability. By using REPL, we can easily code and test Java-based logic without assembling using javac and you can see the result of calculations directly.

Running JShell

Open a command prompt and write jshell

$ jshell
'  Welcome to JShell -- Version 9-ea
'  For an introduction type: /help intro
jshell>

Seeing JShell Commands

Write/help one time when jshell commands start running.

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>
'  edit a source entry referenced by name or id
'  /drop <name or id>
'  delete a source entry referenced by name or id
'  /save [-all'-history'-start] <file>
'  Save snippet source to a file.
'  /open <file>
'  open a file as source input
'  /vars [<name or id>'-all'-start]
'  list the declared variables and their values
'  /methods [<name or id>'-all'-start]
'  list the declared methods and their signatures
'  /types [<name or id>'-all'-start]
'  list the declared types
'  /imports 
'  list the imported items

Running JShell Command

Write /import one time when jshell commands start running and view the used imports.

jshell> /imports
'    import java.io.*
'    import java.math.*
'    import java.net.*
'    import java.nio.file.*
'    import java.util.*
'    import java.util.concurrent.*
'    import java.util.function.*
'    import java.util.prefs.*
'    import java.util.regex.*
'    import java.util.stream.*
jshell>

Running Calculations in JShell

Try running easy calculations in JShell.

jshell> 3+1
$1 ==> 4
jshell> 13%7
$2 ==> 6
jshell> $2
$2 ==> 6
jshell>

Creating and using functions in JShell

Generate a function doubled() to take an int and return its doubled value.

jshell> int doubled(int i){ return i*2;}
'  created method doubled(int)
jshell> doubled(6)
$3 ==> 12
jshell>

ExitingJShell

Write/exit.

jshell> /exit
' Goodbye