Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java Interfaces

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Class References

Java Useful Resources

Java - REPL (JShell)



Introduction to REPL (JShell)

REPL stands for Read Evaluate Print Loop. JShell was introduced in Java 9 which is an interactive console. JShell as REPL allows to run arbitrary snippet of java code in console without need to save and compile java code file. This facility is very important to test codes quickly like evaluating regular expression, checking formating of strings, date formats etc.

JShell reads each line entered, evaluates it and then print the result and then again becomes ready for next set of input.

Advantages of Using JShell

This capability of JShell gives developers following advantages -

  • No editor is needed to write a Java program. JShell itself works as editor and executes the Java code.

  • Using JShell, there is no requirement to save a Java file, compile and execute code cycle. Code can be directly tested in JShell without saving anything.

  • Compilation is not needed prior to execution of code.

  • If any compile-time or runtime error occurs, start fresh.

Running JShell

Open command prompt and type jshell.

D:\test>jshell
|  Welcome to JShell -- Version 20.0.2
|  For an introduction type: /help intro

With JShell, we can test methods, classes, expressions as well. In following examples, let's explore some of the features of JShell.

Create and Invoke Method in JShell

Following snippet showing a sample "Hello World" program in JShell. Here, we've created a method greet() which has a single statement to print a message as "Hello World!". As next, we've invoked the method greet() and result is printed on the console.

Hello World Example in JShell

jshell> void greet() { System.out.println("Hello World!");}
|  created method greet()

jshell> greet()
Hello World!
jshell>

Creating Variables in JShell

Following snippet shows how to create variables in JShell. semi-colon is optional. We can create objects as well in JShell. If a variable is not initialized then it is given a default value or null if it is an object reference. Once a variable is created, it can be used as shown in the last statement where we've used the string variable to print its value.

Example

jshell> int i = 10
i ==> 10

jshell> String name = "Mahesh";
name ==> "Mahesh"

jshell> Date date = new Date()
date ==> Fri Feb 02 14:52:49 IST 2024

jshell> long l
l ==> 0

jshell> List list
list ==> null

jshell> name
name ==> "Mahesh"

Evaluate Expression in JShell

Following snippet shows how to evaluate an expression using JShell. Here we've passed a statement which returns a formatted string. JShell automatically created a String variable $9 and assigned it the result. As next statement, we've printed.

Example

jshell> String.format("%d pages read.", 10);
$9 ==> "10 pages read."

jshell> $9
$9 ==> "10 pages read."

Jshell Built-In Commands

JShell provides various commands to list the variables created, methods created, imports used etc. Some of the important JShell commands are -

  • /drop – This command drops code snippets identified by name, ID, or ID range.
  • /edit – This command opens an editor.
  • /env – This command displays the environment settings.
  • /exit – This command exists from the tool.
  • /history – This command displays the history of the tool.
  • /help – This command displays the command's help.
  • /imports – This command displays the current active imports.

Example: Demonstrating /help Command

We can view all commands using /help option.

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
|  /drop <name or id>
|       delete a source entry
|  /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 type declarations

...

Example: Demonstrating /vars Command

In following example, we've used /vars command to print the variables declared during a session.

C:\Users\Mahesh>jshell
|  Welcome to JShell -- Version 20.0.2
|  For an introduction type: /help intro

jshell> int i = 10
i ==> 10

jshell> String name="Mahesh"
name ==> "Mahesh"

jshell> /vars
|    int i = 10
|    String name = "Mahesh"

jshell>

Example: Demonstrating /imports Command

We can use /imports command to check the imports available in JShell as shown below:

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>

Exiting JShell

We can use /exit command to exit JShell as shown below:

Example

jshell> /exit
|  Goodbye

C:\Users\Mahesh>
Advertisements