JShell : Get Coding with Java Interactively

JShell is a powerful interactive tool built into Java 9 and above. It lets you experiment with Java code line-by-line, evaluate expressions, and explore the language without setting up full projects. JShell is a REPL (Read Evaluate Print Loop) tool and run from the command line.

This tutorial will guide you through the basics of using JShell.

Getting Started

  1. Make sure you have Java 9 or later installed. Check your Java version by running java -version in your terminal. If you don’t have it, download and install it from the official Java website.

  2. Open a terminal or command prompt.

  3. Start JShell by typing jshell and pressing Enter.

Basic Guidelines

  • Enter Java expressions and statements: Type them like you would in a regular Java program and press Enter. JShell will evaluate them and show the result.
  • No semicolons needed: Unlike in traditional Java, semicolons are optional for individual statements in JShell.
  • Tab completion: Press Tab to get autocomplete suggestions for variables, classes, and methods.
  • History navigation: Use the up and down arrows to scroll through your previous commands.

Exploring Features

  • Declare variables: Define variables using int x = 10; or String name = "John";.
  • Use operators: Perform calculations, comparisons, and other operations like x + 5 or name.length().
  • Call methods: Use existing methods like System.out.println("Hello") or custom methods you define.
  • Define classes and methods: Create classes and methods on the fly, experimenting with code snippets.
  • Import libraries: Use the /import command to import external libraries, e.g., /import java.util.Scanner;.
  • Run scripts: Save code snippets in a file and run them with /open <filename>.

Executing Java code

We have already seen how to launch JShell in ‘Getting Started’ section above. Lets deep dive into other important operations.

Using ‘variables’

We can declare variables and use anywhere throughout Jshell session. Let’s create an integer variable.

				
					jshell> int a = 10;
jshell> int b = 20;
jshell> int sum = a + b;
jshell> System.out.println("Sum: " + sum);
				
			

Scratch Variables

When an expression is entered that doesn’t have a named variable, a scratch variable is created so that the value can be referenced later. The following example shows scratch values for an expression and for the results of a method. The example also shows the continuation prompt (...>) that is used when a snippet requires more than one line of input to complete:

				
					jshell> 2 + 2
$3 ==> 4
|  created scratch variable $3 : int

jshell> String twice(String s) {
   ...>    return s + s;
   ...> }
|  created method twice(String)

jshell> twice("Ocean")
$5 ==> "OceanOcean"
|  created scratch variable $5 : String
				
			

Exploring History and Editing Commands

JShell maintains a command history that you can navigate using the up and down arrow keys. You can also edit previous commands using an external editor by typing /edit.

Importing Classes and Packages

You can import classes and packages in JShell using the import command. For example:

				
					jshell> import java.util.*;
jshell> List<String> list = new ArrayList<>();
				
			

Using External Dependencies

JShell allows you to load external dependencies (JAR files) dynamically using the /open command.

For example:

				
					jshell> /open gson-2.8.6.jar
jshell> import com.google.gson.Gson;

				
			

Saving and Loading JShell Sessions

You can save your JShell session to a file using the /save command and load it later using the /open command.

For example:

				
					jshell> /save mysession.jsh
jshell> /exit
(exit JShell)

				
			
				
					jshell> /open mysession.jsh

				
			

Exiting Shell

To exit JShell, type the /exit command or press Ctrl + D.

Conclusion

JShell provides a convenient and interactive environment for experimenting with Java code without the need for a full-fledged development environment. By following this tutorial and practicing with JShell, you can enhance your Java programming skills and explore Java concepts in a hands-on manner. So, fire up JShell and start exploring the world of Java interactively!

Leave a Comment

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