Complete guide to JShell: The underrated Java Playground available with JDK 9 onwards

JShell is a Java REPL (Read-Eval-Print Loop) tool that allows you to experiment with Java code snippets and quickly see the results. With JShell, you can interactively write and test Java code without needing to create a full Java program, compile it, and run it. JShell is included in the JDK (Java Development Kit) starting from version 9.

Benefits of using JShell

  1. Interactive experimentation: JShell allows you to quickly experiment with Java code and see the results in real time. You can test individual statements or entire programs without having to compile and run them in a separate environment.
  2. Immediate feedback: JShell provides immediate feedback on each statement you enter, which can help you to identify and fix errors more quickly.
  3. Increased productivity: With JShell, you can quickly test and prototype code without the overhead of setting up a project or writing boilerplate code. This can help you to be more productive and iterate more quickly.
  4. Learning tool: JShell can be a powerful tool for learning Java. It provides a safe and interactive environment to experiment with code and learn new concepts.
  5. Debugging: JShell can also be used as a debugging tool. You can use it to quickly test code snippets or diagnose issues with your code.

Let’s explore the JShell concepts and usage in detail. We will start from the basics and cover the advanced usage, features, tips & tricks and some practical real-world example use-cases.

1. Basics

JShell is a tool that allows you to execute Java code snippets and get immediate feedback without having to write a complete Java program. It is a Read-Eval-Print Loop (REPL) environment that provides an interactive shell for experimenting with Java language constructs and APIs.

How to start JShell

To start JShell, open a terminal or command prompt and type jshell. This will launch the JShell prompt where you can enter Java code snippets and see their results.

Basic usage and commands

Once you are at the JShell prompt, you can enter Java code snippets and see their results immediately. For example:

jshell> 2 + 3
$1 ==> 5

In this example, we entered the expression 2 + 3 and JShell evaluated it to be 5. JShell also assigned the result to a special variable called $1. You can refer to this variable later in your session.

JShell provides several commands that you can use to control the environment and display information. For example, you can use the /help command to display a list of available commands:

jshell> /help
|  Expression control commands:
|    /edit <id>                    - edit a previously input expression
|    /drop [<id> | <name>]         - remove an entry or entries
|    /save [-all|-history] <file>  - save snippet(s) to a file
|    /open <file>                  - open a file as input
|    /vars                          - list the declared variables and their values
|    /methods                       - list the declared methods and their signatures
|    /types                         - list the declared types
|    /imports                      - list the imported packages
|    /exit                          - exit JShell

You can use the /vars command to display a list of declared variables, the /methods command to display a list of declared methods, and the /types command to display a list of declared types.

Understanding the JShell prompt

The JShell prompt is where you enter Java code snippets. The prompt is indicated by the jshell> text. Each time you enter a code snippet, JShell evaluates it and displays the result on the next line. If the code snippet has a side effect (such as declaring a variable or printing output), JShell will display the side effect as well.

2. Data Types and Variables

Java has several built-in data types, including int, double, boolean, char, and others. JShell supports all of these data types, as well as user-defined types.

How to declare and initialize variables in JShell

To declare a variable in JShell, you simply enter the variable name and its initial value. For example:

jshell> int x = 5
x ==> 5

In this example, we declared a variable x of type int and assigned it the value 5. JShell evaluated the assignment expression and displayed the value of x on the next line.

You can also declare variables without initializing them:

jshell> int x;
x ==> 0

In this example, we declared a variable x of type int without initializing it. JShell automatically initialized x to the default value for int, which is 0. You can update the value of a variable by re-assigning it:

jshell> x = 10;
    x ==> 10

In this example, we re-assigned the value of `x` to `10`. JShell evaluated the assignment expression and displayed the new value of `x` on the next line.

Using JShell as a calculator

JShell can be used as a simple calculator for basic arithmetic operations. For example, you can add, subtract, multiply, and divide numbers:

jshell> 2 + 3
    $1 ==> 5
    
    jshell> 7 - 4
    $2 ==> 3
    
    jshell> 5 * 2
    $3 ==> 10
    
    jshell> 10 / 2
    $4 ==> 5

In these examples, we entered basic arithmetic expressions and JShell and evaluated them to their results. JShell assigned each result to a special variable (`$1`, `$2`, `$3`, and `$4`).

3. Control Flow Statements

Let’s see the control flow statements in the Jshell using if-else if statements and loops.

Using JShell to write and test if-else statements

JShell allows you to write and test if-else statements interactively. For example:

 jshell> int x = 5;
    x ==> 5
    
    jshell> if (x > 0) {
    ...> System.out.println("x is positive");
    ...> } else {
    ...> System.out.println("x is non-positive");
    ...> }
    x is positive

In this example, we declared a variable `x` and assigned it the value `5`. We then wrote an if-else statement that tested if `x` is greater than `0`. If the test was true, we printed the message `”x is positive”`. Otherwise, we printed the message `”x is non-positive”`.

Writing and testing for loops and while loops in JShell

JShell also allows you to write and test loops interactively. For example:

jshell> for (int i = 0; i < 5; i++) {
    ...> System.out.println(i);
    ...> }
0
1 
2
3
4

jshell> int i = 0;
i ==> 0

jshell> while (i < 5) {
...> System.out.println(i);
...> i++;
...> }
0
1
2
3
4

4. Methods

In Java, a method is a block of code that performs a specific task. In JShell, you can write and test methods without having to create a complete Java program.

Writing and testing simple methods in JShell

JShell allows you to define and test methods interactively. For example, let’s define a method that takes two integers as input and returns their sum:

jshell> int add(int a, int b) {
...>     return a + b;
...> }
|  created method add(int,int)

jshell> add(2, 3)
$1 ==> 5

In this example, we defined a method add that takes two int arguments a and b, adds them together, and returns the result. We then called the method with arguments 2 and 3, and JShell returned the result 5.

Creating methods with parameters and return values

You can create more complex methods that take multiple arguments and return more complex data types. For example, let’s define a method that takes a string as input, converts it to uppercase, and returns the result:

jshell> String toUpperCase(String str) {
...>     return str.toUpperCase();
...> }
|  created method toUpperCase(String)

jshell> toUpperCase("hello, world!")
$2 ==> "HELLO, WORLD!"

In this example, we defined a method toUpperCase that takes a String argument str, converts it to uppercase using the toUpperCase method of the String class, and returns the result. We then called the method with the argument "hello, world!", and JShell returned the result "HELLO, WORLD!".

Note that you can also define methods with no return value by using the void keyword instead of a data type in the method signature. These methods perform some action but do not return a value.

jshell> void printMessage(String message) {
...>     System.out.println(message);
...> }
|  created method printMessage(String)

jshell> printMessage("Hello, JShell!")
Hello, JShell!

In this example, we defined a method printMessage that takes a String argument message, prints the message to the console using the println method of the System.out object, and does not return a value. We then called the method with the argument "Hello, JShell!", and JShell printed the message to the console.

5. Object-Oriented Programming

JShell is a powerful tool that provides a flexible environment to experiment with Object-Oriented Programming concepts. Here are some examples of how JShell can be used to create classes and objects, use constructors, instance methods, and instance variables, and experiment with class design:

Creating classes and objects in JShell

JShell allows you to experiment with creating and using classes and objects interactively. For example, let’s define a simple class called Person that has an instance variable name and an instance method greet that prints a greeting to the console:

jshell> class Person {
...>     String name;
...>     void greet() {
...>         System.out.println("Hello, my name is " + name);
...>     }
...> }
|  created class Person

jshell> Person john = new Person()
john ==> Person@7f6906c4

In this example, we defined a class Person that has an instance variable name of type String and an instance method greet that prints a greeting to the console using the println method of the System.out object. We then created an object john of type Person using the new keyword, and assigned it to a variable john. JShell returned the object reference Person@7f6906c4.

Using constructors, instance methods, and instance variables

Once you have created an object of a class, you can access its instance variables and methods using the dot notation. For example, let’s set the name instance variable of the john object to "John Smith", and call its greet method:

jshell> john.name = "John Smith"
john.name ==> "John Smith"

jshell> john.greet()
Hello, my name is John Smith

In this example, we set the name instance variable of the john object to "John Smith" using the dot notation, and JShell returned the value of the name variable. We then called the greet method of the john object using the dot notation, and JShell printed the greeting to the console.

You can also define constructors for your classes to initialize instance variables when an object is created. For example, let’s define a constructor for the Person class that takes a String argument name and initializes the name instance variable:

jshell> class Person {
...>     String name;
...>     Person(String n) {
...>         name = n;
...>     }
...>     void greet() {
...>         System.out.println("Hello, my name is " + name);
...>     }
...> }
|  modified class Person

jshell> Person jane = new Person("Jane Doe")
jane ==> Person@7e1a9f4d

In this example, we modified the Person class to include a constructor that takes a String argument n and initializes the name instance variable to the value of n. We then created an object jane of type Person using the constructor with the argument "Jane Doe", and assigned it to a variable jane. JShell returned the object reference Person@7e1a9f4d.

Using JShell to experiment with class design

JShell provides an interactive environment to experiment with different class designs. For example, you can create a “Rectangle” class with instance variables “width” and “height”, and instance methods to calculate its area and perimeter:

jshell> public class Rectangle {
   ...>     double width;
   ...>     double height;
   ...>     
   ...>     public Rectangle(double width, double height) {
   ...>         this.width = width;
   ...>         this.height = height;
   ...>     }
   ...>     
   ...>     public double getArea() {
   ...>         return width * height;
   ...>     }
   ...>     
   ...>     public double getPerimeter() {
   ...>         return 2 * (width + height);
   ...>     }
   ...> }

Now, you can create an object of the “Rectangle” class and call its instance methods to get its area and perimeter:

jshell> Rectangle r = new Rectangle(5.0, 10.0);
jshell> r.getArea();
50.0
jshell> r.getPerimeter();
30.0

6. Java APIs and External Libraries

JShell is a great tool for experimenting with Java APIs and external libraries, allowing you to import and use them directly in your code. Here are some examples of how to use Java APIs and external libraries in JShell for working with dates, strings, and collections:

Importing and Using Java APIs and External Libraries in JShell:

JShell allows you to easily import and use Java APIs and external libraries using the “import” keyword. Here’s an example of how to import the “java.util” package and use its “ArrayList” class to create a list of integers:

jshell> import java.util.ArrayList;
jshell> ArrayList<Integer> list = new ArrayList<>();
jshell> list.add(1);
jshell> list.add(2);
jshell> list.add(3);
jshell> list
list ==> [1, 2, 3]

Examples of Using APIs for Working with Dates, Strings, and Collections:

Java APIs provide a wide range of functionality for working with dates, strings, and collections. Here are some examples of how to use APIs for these purposes in JShell:

Working with Dates:

You can use the “java.time” package to work with dates and times in Java. Here’s an example of how to create a date and format it using the “DateTimeFormatter” class:

jshell> import java.time.LocalDate;
jshell> import java.time.format.DateTimeFormatter;
jshell> LocalDate date = LocalDate.now();
jshell> DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
jshell> formatter.format(date)
$1 ==> "08/04/2023"

Working with Strings

You can use the “java.lang.String” class and its methods to manipulate strings in Java. Here’s an example of how to create a string, replace a character, and convert it to uppercase:

jshell> String str = "Hello, world!";
jshell> str = str.replace(',', ' ');
jshell> str = str.toUpperCase();
jshell> str
str ==> "HELLO  WORLD!"

Working with Collections

You can use the “java.util” package and its collection classes to work with collections in Java. Here’s an example of how to create a set of integers and iterate over its elements:

jshell> import java.util.HashSet;
jshell> Set<Integer> set = new HashSet<>();
jshell> set.add(1);
jshell> set.add(2);
jshell> set.add(3);
jshell> for (int i : set) {
   ...>     System.out.println(i);
   ...> }
1
2
3

Overall, JShell provides a convenient way to experiment with Java APIs and external libraries and test their functionality in real-time.

7. Debugging with JShell

JShell provides a powerful tool for debugging Java code, allowing developers to diagnose and fix errors in real-time. Here are some examples of how to use JShell to debug Java code, diagnose and fix errors, and troubleshoot common problems.

Using JShell to Debug Java Code:

JShell provides a useful environment for debugging Java code, allowing developers to experiment with different solutions and test them in real-time. Here’s an example of how to use JShell to debug a simple Java program:

jshell> int x = 5;
jshell> int y = 0;
jshell> int z = x / y;
|  java.lang.ArithmeticException thrown: / by zero
|        at (#2:1)

In this example, we create two variables “x” and “y”, and then try to divide “x” by “y”. JShell throws an “ArithmeticException” because we are trying to divide by zero. We can then use JShell to experiment with different solutions and test them in real-time.

Diagnosing and Fixing Errors with JShell:

JShell provides a powerful tool for diagnosing and fixing errors in Java code. Here’s an example of how to use JShell to diagnose and fix an error in a simple Java program:

jshell> int x = 5;
jshell> int y = 2;
jshell> int z = x / y;
jshell> System.out.println(z);
2
jshell> String str = "Hello, world!";
jshell> int length = str.length();
jshell> System.out.println(length);
13

In this example, we create two variables “x” and “y”, and then divide “x” by “y” to get the value of “z”. We then print the value of “z” using the “System.out.println()” method. We also create a string “str” and get its length using the “length()” method, and then print the length of the string. JShell allows us to diagnose and fix errors in real-time, allowing us to experiment with different solutions until we find the right one.

Troubleshooting Common Problems in JShell:

JShell provides a convenient environment for troubleshooting common problems that developers may encounter when working with Java code. Here are some examples of common problems and how to troubleshoot them in JShell:

  • Syntax errors: If you encounter a syntax error in JShell, check your code for typos or missing semicolons. JShell will often provide a helpful error message that will point you in the right direction.
  • Variable scope errors: If you encounter a variable scope error in JShell, make sure that you have declared the variable in the correct scope. JShell allows you to experiment with different solutions in real time, so you can quickly identify and fix variable scope errors.
  • NullPointerExceptions: If you encounter a NullPointerException in JShell, check your code for null values. You can use JShell to experiment with different solutions and test them in real-time to diagnose and fix the problem.

8. Advanced JShell Features

JShell comes with several advanced features that can make the development process faster and more efficient. Here are some examples of advanced features in JShell:

Multi-line editing:

JShell allows developers to edit multi-line code snippets using the editor’s built-in multi-line mode. Developers can use the up and down arrow keys to navigate through the code, edit it, and execute it when they’re done. Here’s an example:

jshell> public class MyClass {
   ...>    public void myMethod() {
   ...>        System.out.println("Hello, world!");
   ...>    }
   ...> }

Auto-completion:

JShell comes with an auto-completion feature that can save developers a lot of time when typing code. This feature suggests code completions based on the context, making it easier to type complex code quickly. Here’s an example:

jshell> String s = "Hello, world!";
s ==> "Hello, world!"
jshell> s.leng
s.length(  
jshell> s.length()
$3 ==> 13

In this example, we’re using auto-completion to quickly type the “length()” method for a string. JShell suggests the completion based on the context and allows us to quickly execute the code.

Saving and loading JShell sessions:

JShell allows developers to save their JShell sessions to a file and load them later. This feature can be helpful when working on a complex project that requires multiple iterations. Here’s an example:

jshell> /save mysession.jsh
jshell> /exit
$ jshell mysession.jsh

In this example, we’re using the “/save” command to save our JShell session to a file called “mysession.jsh”. We then exit JShell and reload the session from the file using the “jshell” command with the file name as an argument.

Feedback mode

JShell feedback mode is a feature that allows you to see the result of an expression immediately after typing it, without having to press enter. This can be useful for quickly testing and exploring code.

To enter feedback mode in JShell, you can use the /set feedback command followed by the mode you want to use. There are three modes available:

  • silent: No feedback is displayed.
  • verbose: Detailed feedback is displayed for each expression, including the value of the expression and any output or error messages.
  • normal: Only the value of the expression is displayed.

Here’s an example of how to use feedback mode in JShell:

jshell> /set feedback normal
jshell> 2 + 3
$1 ==> 5

jshell> "Hello, World!"
$2 ==> "Hello, World!"

jshell> /set feedback verbose
jshell> Math.sqrt(25)
|  Expression value is: 5.0
$3 ==> 5.0

jshell> /set feedback silent
jshell> System.out.println("This is a test")
$4 ==> void

In this example, we used the /set feedback command to switch between different feedback modes. In normal mode, only the value of the expression is displayed. In verbose mode, detailed feedback is displayed for each expression, including the value of the expression and any output or error messages. In silent mode, no feedback is displayed at all.

Feedback mode can be a useful tool for quickly testing and exploring code in JShell, and it can help you to quickly identify any issues with your code.

9. Tips and Tricks

Here are some tips and tricks for using JShell:

Exiting JShell:

To exit JShell, simply type “/exit” or press Ctrl + D.

Clearing and resetting the session:

To clear the current session in JShell, type “/reset”. This command will clear all variables, methods, and classes from the current session. To start a new session from scratch, type “/restart”.

Viewing JShell documentation:

To view documentation for a specific class or method in JShell, use the “/doc” command followed by the class or method name. For example, “/doc String” will display the documentation for the String class.

Common JShell errors and how to fix them: Some common errors that you might encounter while using JShell include syntax errors, undefined variables, and null pointer exceptions. To fix syntax errors, carefully review your code and make sure all syntax is correct. To fix undefined variables, make sure you have defined the variable before using it. To fix null pointer exceptions, check that any variables or objects being accessed are not null.

Best practices for using JShell:

Here are some best practices to follow when using JShell:

  • Use JShell to experiment with code and explore new Java features.
  • Use JShell to quickly test small snippets of code before adding them to a larger project.
  • Use JShell to debug code and diagnose errors.
  • Use JShell to learn Java syntax and programming concepts.
  • Use JShell with external libraries and APIs to quickly test and prototype code.

10. Practical Examples

Using JShell to solve real-world problems:

JShell can be used to quickly prototype and test solutions to real-world problems. For example, if you need to write a program to calculate the average temperature for a given set of days, you can use JShell to quickly test different calculations and see the results.

Example

Suppose you have a list of temperatures for a given week, and you want to calculate the average temperature for the week. Here’s how you can use JShell to do this:

jshell> List<Double> temperatures = Arrays.asList(23.5, 25.2, 24.8, 22.1, 21.5, 20.9, 22.6)
temperatures ==> [23.5, 25.2, 24.8, 22.1, 21.5, 20.9, 22.6]

jshell> double sum = temperatures.stream().mapToDouble(Double::doubleValue).sum()
sum ==> 160.6

jshell> double avg = sum / temperatures.size()
avg ==> 22.942857142857143

Working with JSON data:

JShell can be used to manipulate and analyze JSON data. You can use external libraries like Jackson or Gson to parse JSON data and create Java objects. Then, you can use JShell to experiment with different ways of processing and manipulating the data.

Example

Suppose you have a JSON string representing information about a person, and you want to parse it into a Java object. Here’s how you can use JShell to do this using the Jackson library:

jshell> String json = "{\"name\":\"John Smith\", \"age\":35, \"email\":\"john@example.com\"}"
json ==> "{\"name\":\"John Smith\", \"age\":35, \"email\":\"john@example.com\"}"

jshell> ObjectMapper mapper = new ObjectMapper()
mapper ==> com.fasterxml.jackson.databind.ObjectMapper@6d5e6b37

jshell> Person person = mapper.readValue(json, Person.class)
person ==> Person{name='John Smith', age=35, email='john@example.com'}

Automating repetitive tasks:

JShell can be used to automate repetitive tasks, such as generating reports or processing large amounts of data. For example, you can write a JShell script to read data from a CSV file, perform some calculations, and generate a report in a specific format.

Example

Suppose you have a directory with a large number of files, and you want to rename all the files to include the current date in the file name. Here’s how you can use JShell to do this:

jshell> SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd")
formatter ==> java.text.SimpleDateFormat@7cc355be

jshell> File directory = new File("/path/to/directory")

jshell> Arrays.stream(directory.listFiles()).forEach(file -> {
           String filename = file.getName();
           String date = formatter.format(new Date());
           String newFilename = date + "-" + filename;
           File newFile = new File(directory.getAbsolutePath() + File.separator + newFilename);
           file.renameTo(newFile);
      })

Testing Java code snippets:

JShell can be used to test Java code snippets before integrating them into larger projects. For example, you can test a new method or class in JShell to make sure it works as expected before integrating it into a larger project.

Example

Suppose you have written a method to calculate the factorial of a number, and you want to test it to make sure it works correctly. Here’s how you can use JShell to do this:

jshell> int factorial(int n) {
           if (n == 0) {
               return 1;
           } else {
               return n * factorial(n - 1);
           }
      }
|  created method factorial(int)

jshell> factorial(5)
$3 ==> 120

Footnotes

In conclusion, JShell is a powerful tool for experimenting with Java code, testing ideas, and exploring new programming concepts. With its interactive and user-friendly interface, JShell makes it easy to quickly test and prototype code without the need to write lengthy programs or set up complex development environments.

JShell is particularly useful for solving real-world problems, working with JSON data, automating repetitive tasks, and testing Java code snippets. Its ability to import and use external libraries and APIs, and to work with advanced features like multi-line editing and auto-completion, makes it an indispensable tool for any Java programmer.

By following best practices and using JShell effectively, you can improve your Java programming skills and work more efficiently. Whether you are a beginner or an experienced developer, JShell is a valuable tool that can help you solve problems and streamline your workflow. So why not give JShell a try today and see how it can benefit your Java programming projects?

You may also like...

Leave a Reply

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