Quick Summary:

  • JShell – the Java Shell Tool
  • An interactive command-line interface for Java programming, it allows for immediate feedback by executing Java statements, expressions, and declarations incrementally.
  • Facilitates rapid testing and prototyping without the need for full applications or IDEs.
  • Released in Java 9 (2017).

Key Features

  • JShell is part of the Java SE Platform and is included in the JDK.
  • It is designed to enhance the learning experience of Java by providing a platform for experimenting and testing code snippets quickly.
  • With JShell, users can execute Java code snippets, including variables, expressions, statements, methods, and classes, and get immediate results.
  • This makes it a powerful tool for developers looking to test ideas or debug code in real time.

👩‍💻 Hands-on Demo: JShell

  1. Start JShell by opening your terminal and running jshell.

  2. Try declaring some variables of different types:
    • A first name.
    • A last name.
    • A date of birth.
    • An age (based on the date of birth).
    • A weight.
    • A height.
    • A BMI score (based on the weight and height).
  3. Print a sentence like “My name is James Barnes, I’m 53-years-old and I have a BMI of 23.3”, using the aforementioned variables.
  4. Write a simple method in JShell to calculate the factorial of a number and test it interactively.
  5. Introduce deliberate errors into your JShell commands to observe how errors are reported. Modify your commands to correct these errors.
  6. Exit by using any of the following:
    • Enter the command: /exit
    • (Windows) Use the Ctrl+D shortcut.


🕵️‍♂️ Click here to reveal the solutions