JShell - learning Java made easy

JShell - learning Java made easy

What is JShell and why it is a big deal

The Java Shell or simply JShell is an interactive tool for learning the Java programming language. It has been introduced in Java 9. What this does is, we can test new features right in the command line with out a need to create a separate class. For example, to print the current system time, we can simply call the static method java.time.LocalDateTime.now() in the command line. jshell sample

The world before jshell

Before Java 9, to learn something or to teach something as simple as printing “Hello world”, one has to create a new class and perform all the boilerplate code needed to create a new class, method etc

To simply print “Hello world!”


public class HelloWorld{
	public static void main(String[] args){
		System.out.println("Hello world!");

To get the current time,


import java.time.LocalDateTime
public class CurrentTime.java{
	public static void main(String[] args){

The problem here is, people who are new to Java or even new to programming, this is overwhelming. They either have to be aware of object oriented programming, classes, methods, static function or we should ask them to ignore everything and only concentrate on the println statement.

Even for people who want to simply test a new feature, they have to create a new class, main method, compile this class and run it.

But with the introduction of JShell this is extremely simplified.

After the introduction of JShell we can simply type the following to get the current system time,

$12 ==> 2018-05-25T13:22:11.813

Here the “$12” is a temporary/scratch variable and its value is “2018-05-25T13:22:11.813”. If we type “$12” again we get the original value that was assigned to it earlier


Another example of using this shell, to print “Hello world”

jshell> System.out.println("Hello world")
Hello world

This feature is truly valuable when it comes to teaching as well as checking out the existing features quickly.

Starting and stopping the JShell

  • Open your command prompt or terminal
  • Make sure you have installed JDK 9 or above. You can check this by “java -version” command
  • Simply type “JShell” and press enter. This is case insensitive. JSHELL, jSHELL, JShell are all same.
  • To stop/exit out of JShell type “/exit”
  • “jshell -v” starts jshell in verbose mode. This provides commentary of each and every step that is executed. This can be changed by /set feedback silent or /set feedback concise
  • JShell accepts Java statements, variables, methods, class definitions, imports and expressions.


jshell> int x = 100
x ==> 100

Note: Semicolons are automatically added to the end of a complete snippet if not entered. The following statements are same,

jshell> int x = 100
jshell> int x = 100;


jshell> 101 + 101
$4 ==> 202

We can create methods too

jshell> int sum(int a, int b){
   ...> return a + b;
   ...> }
jshell>String decorate(String s){
  ...> return "Hello"+s;

We can call these methods, just like we would normally do,

$8 ==> "Hello jshell"

We can change the definition of a method by simply redefining the method

jshell>String decorate(String s){ 
	..> return "Mr. "+s; 
	..> }
jshell> decorate("Sun")
$10 ==> "Mr. Sun"

Now we can simply open up our terminal, type a Java api and see how it works. Sure, most of us have a class where we type the stuff we want to check and see the results. JShell just makes it easier. As I have told before, JShell is truly a gifted tool when it comes to teaching. If you are teaching someone, you do not have to go through whole lot of steps to get the point across.

Check this link from oracle, where they have an excellent introduction to JShell.