Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Getting Started with JShell Part 2

Learn the basics of JShell, a Read-Evaluate-Print-Loop command line tool, including executing simple statements, defining classes, and commands to manage code snippets.

Dec 14, 2018 • 9 Minute Read

Preface

In the first guide of this series, you learned what JShell is and how to install it.

In this guide, you'll learn how to start working with Jshell.

Expressions and Variables

So, let's start with a simple expression:

      jshell> "Hello World"
$1 ==> "Hello World"
    

Notice that the result of the evaluation is shown and it's assigned to the variable named $1. You can check this by entering the name of the variable:

      jshell> $1
$1 ==> "Hello World"
    

A variable like this will be created implicitly when the result of an expression is not assigned to some variable. If the expression doesn't have a return value (like a print statement), no variable will be created.

You can use this implicit variable like any other:

      jshell> System.out.println($1)
Hello World
    

You can also change the value it holds:

      jshell> $1 = $1 + " again"
$1 ==> "Hello World again"

jshell> System.out.println($1)
Hello World again
    

However, it's probably better to work with some meaningful identifiers:

      jshell> String hello = $1
hello ==> "Hello World again"

jshell> System.out.println(hello)
Hello World again
    

As you can see in the previous examples, it's not required to use a semicolon for single statements. Of course, this will not work if you try to execute more than one statement at the same time:

      jshell> int sum = 1 + 2 System.out.println(sum)
|  Error:
|  ';' expected
|  int sum = 1 + 2 System.out.println(sum);
|                 ^
    

You'll need a semicolon between statements (but not after the last one, if you don't want):

      jshell> int sum = 1 + 2; System.out.println(sum)
sum ==> 3
3
    

Also, JShell won't execute a statement until it knows the statement is completed. For example, if you only type if(sum > 0), the prompt will change to ...>:

      jshell> if(sum >0)
   ...>
    

We'll be prompted to continue entering snippets until a valid statement or block is completed:

      jshell> if(sum > 0)
   ...> System.out.print(sum)
3
    

Methods

The capability of typing multi-line statements allows us to write complete methods and classes. For example, you can define a method to return the negative value of the argument:

      jshell> int getNegativeValue(int number) {
   ...> return -number;
   ...> }
|  created method getNegativeValue(int)
    

This way, you can call it like this:

      jshell> getNegativeValue(10)
$5 ==> -10
    

Referencing

Sometimes, a method references variables or other methods that are defined later in a class. These are called forward references. Since in JShell the code is entered and evaluated sequentially, code that uses forward references cannot be invoked until these references are evaluated. Take the following method declaration for example:

      jshell> int addMagicNumber(int number) {
   ...> return number + MAGIC_NUMBER;
   ...> }
|  created method addMagicNumber(int), however, it cannot be invoked until variable MAGIC_NUMBER is declared
    

You won't be able to use it until MAGIC_NUMBER is defined:

      jshell> System.out.println(addMagicNumber(3))
|  attempted to call method addMagicNumber(int) which cannot be invoked until variable MAGIC_NUMBER is declared

jshell> int MAGIC_NUMBER = 1
MAGIC_NUMBER ==> 1

jshell> System.out.println(addMagicNumber(3))
4
    

JShell supports forward references in:

  • Methods
  • The type of
    • Return statements
    • Parameters
    • Variables

However, it doesn't support forward references in variable initializers (possibly because they need to be executed at the moment the variable is defined):

      jshell> int MAGIC_NUMBER = getMagicNumber()
|  Error:
|  cannot find symbol
|    symbol:   method getMagicNumber()
|  int MAGIC_NUMBER = getMagicNumber();
|                     ^------------^
    

Classes

You can also declare classes, for example:

      jshell> class Book {
   ...> private String title;
   ...> public void setTitle(String title) { this.title = title; }
   ...> public String getTitle() { return title; }
   ...> }
|  created class Book
    

And use them like this:

      jshell> Book book = new Book()
book ==> Book@3dd4520b

jshell> book.setTitle("Java 9");

jshell> System.out.println(book.getTitle())
Java 9
    

Modifiers of methods and fields defined inside a class are applied just like in Java:

      jshell> book.title
|  Error:
|  title has private access in Book
|  book.title
|  ^--------^
    

External Declarations

Declarations outside a class or interface (and declarations of classes and interfaces by themselves) are created under the following rules:

  1. Access modifiers (public, protected, and private) are ignored. All declaration snippets are accessible to all other snippets:
      jshell> private int x = 1;
x ==> 1

jshell> System.out.print(x) // private ignored
1
    
  1. The modifier final is ignored. Changes and inheritance are allowed:
      jshell> final class A {void m() {} }
|  Warning:
|  Modifier 'final'  not permitted in top-level declarations, ignored
|  final class A { void m() {} }
|  ^---^
|  created class A
    
  1. The modifier static is ignored because there isn't a container class (at least none you can use):
      jshell> static char letter = 'A'
|  Warning:
|  Modifier 'static'  not permitted in top-level declarations, ignored
|  static char letter = 'A';
|  ^----^
letter ==> 'A'
    
  1. The modifiers default and synchronized are not allowed:
      jshell> synchronized void method() {}
|  Error:
|  Modifier 'synchronized'  not permitted in top-level declarations
|  synchronized void method() {}
|  ^----------^
    
  1. The modifier abstract is allowed only on classes:
      jshell> abstract void method();
|  Error:
|  Modifier 'abstract'  not permitted in top-level declarations
|  abstract void method();
|  ^------^
    

Exceptions

JShell automatically catches exceptions, display information about them, and allows you to continue the session as if nothing has happened. This works for unchecked exceptions:

      jshell> 1/0
|  java.lang.ArithmeticException thrown: / by zero
|        at (#24:1)

jshell>
    

And checked exceptions, without wrapping them in a try/catch statement:

      jshell> FileInputStream fis = new FileInputStream("nonexistentfile.txt")
|  java.io.FileNotFoundException thrown: nonexistentfile.txt (The system cannot find the file specified)
|        at FileInputStream.open0 (Native Method)
|        at FileInputStream.open (FileInputStream.java:196)
|        at FileInputStream.<init> (FileInputStream.java:139)
|        at FileInputStream.<init> (FileInputStream.java:94)
|        at (#25:1)
    

Finally, to end a session just type /exit:

      jshell> /exit
|  Goodbye

$
    

Saving Files

One problem we have is that all these statements are lost when we exit JShell. Luckily, there's a /save command that can save all commands and snippets to a file. It has the following options:

  • /save <file>: Save the commands and snippets currently active to the provided file path.
  • /save -all <file>: Save all commands and snippets, including overwritten, start-up, and failed commands and snippets to the provided file path.
  • /save -history <file>: Save all commands and snippets run to the provided file.
  • /save -start <file>: Save all commands and snippets that initialized the JShell session to the provided path.

This way, if we execute:

      /save my-session.txt
    

A text file named my-session.txt will be saved in the current directory with the statements you issued in the last session. For example:

      "Hello World"
System.out.println($1)
"Hello World modified";
class Book {
private String title = "<NO TITLE>";
public void setTitle(String title) { this.title = title; }
public String getTitle() { return title; }
public String toString() { return "Book: " + title; }
}
    

This way, if we open a new JShell session and execute the command:

      /open my-session.txt
    

All the statements defined in the text file will be executed:

      jshell> /open my-session.txt
Hello World
    

By the way, /open also works with existing Java source files, for example:

      jshell> /open Book.java
    

Next Steps

In the next guide in this series, you'll learn how to manage code snippets and use JShell to explore libraries.