Snippets in jshell

JShell accepts Java statements; variable, method, and class definitions; imports; and expressions. These pieces of Java code are referred to as snippets.


Trying Out Snippets

Snippets of Java code are entered into JShell and immediately evaluated. Feedback about the results, actions performed, and any errors that occurred is shown. Use the examples in this section to give JShell a try.
Start JShell with the verbose option to get the maximum amount of feedback available

C:\Users\Durgesh>jshell -v
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro

jshell> String str="jshell is amazing"
str ==> "jshell is amazing"
|  created variable str : String

jshell>

Important point

First, the result is shown. Read this as: the variable str has the value "jshell is amazing" . Because you are in verbose mode, a description of what occurred is also shown. Informative messages start with a vertical bar. Notice that both the name and the type of the created variable are shown in above example.

Note: Terminating semicolons are automatically added to the end of a complete snippet if not entered.

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> 10+10
$2 ==> 20
|  created scratch variable $2 : int

jshell> public String sayHello(){
   ...> return "hello";
   ...> }
|  created method sayHello()

jshell> sayHello();
$4 ==> "hello"
|  created scratch variable $4 : String

jshell>

Changing Definitions

As you experiment with code, you might find that the definition of a variable, method, or class isnā??t doing what you want it to do. The definition is easily changed by entering a new one, which overwrites the previous definition.

To change the definition of a variable, method, or class, simply enter a new definition. For example, the twice method that was defined in Trying Out Snippets gets a new definition in the following example:


jshell> public String getName(){
   ...> return "John";
   ...> }
|  created method getName()

jshell> getName();
$2 ==> "John"
|  created scratch variable $2 : String

jshell> public String getName(){
   ...> return "petter";
   ...> }
|  modified method getName()
|    update overwrote method getName()
jshell>

Forward References

JShell accepts method definitions that reference methods, variables, or classes that are not yet defined. This is done to support exploratory programming and because some forms of programming require it.
As an example, if you want to define a method getDetail() for the get detail of a person, then you can enter the following command to create the method which uses another method i. e. getPersonDetail() inside it which is not defined:

jshell> public String getDetail(){
   ...> return "Detail is :"+ getPersonDetail();
   ...> }
|  created method getDetail(), however, it cannot be invoked until method getPersonDetail() is declared

jshell>

JShell allows the definition but warns of what is yet to be defined. The definition can be referenced, but if execution is attempted, then it fails until all of the required elements are defined:

jshell> public String getDetail(){
   ...> return "Detail is :"+ getPersonDetail();
   ...> }
|  created method getDetail(), however, it cannot be invoked until method getPersonDetail() is declared

jshell> getDetail()
|  attempted to call method getDetail() which cannot be invoked until method getPersonDetail() is declared

jshell>
 

Exceptions

In an exception backtrace, feedback identifies the snippet and the location within the snippet where the exception occurred.

The location within the code entered into JShell is displayed as #ID:line-number, where snippet ID is the number displayed by the /list command, and line-number is the line number within the snippet. In the following example, the exception occurs in snippet 1, which is the divide() method, on the second line of the method:

jshell> public void divide(int a, int b){
   ...> int c=a/b;
   ...> System.out.println(c);
   ...> }
|  created method divide(int,int)

jshell> divide(5,0);
|  java.lang.ArithmeticException thrown: / by zero
|        at divide (#1:2)
|        at (#2:1)

jshell> /list

   1 : public void divide(int a, int b){
       int c=a/b;
       System.out.println(c);
       }
   2 : divide(5,0);

jshell>

Tab Completion for Snippets

When you enter snippets, use the Tab key to automatically complete the item. If the item can not be determined from what was entered, then possible options are provided.

For example, if you entered the length() method from Forward References, then you can enter the first few letters of the method name, and then press the Tab key to complete the entry:

jshell  "mystrong" . len 

input changes to :
jshell> "durgesh".length()
length()

jshell> "durgesh".length()

jshell> String word="I love java";
word ==> "I love java"
|  created variable word : String

jshell> word.length()
length()

jshell> word.length()
jshell> word.length();
$4 ==> 11
|  created scratch variable $4 : int

jshell>

Snippet Transformation

JShell makes it easy to import a needed class when it is first referenced and convert an expression to a variable declaration using keyboard shortcuts.

When you enter an identifier that is not imported yet, press Shift+Tab i immediately after the identifier to see the options that enable you to add the import to your session:

jshell> new JFrame  //press shift+tab then i
0: Do nothing
1: import: javax.swing.JFrame
Choice:
Imported: javax.swing.JFrame

jshell> new JFrame("Demo")
jshell> JFrame myFrame = new JFrame("Demo")
myFrame ==> javax.swing.JFrame[frame0,0,0,0x0,invalid,hidden, ... tPaneCheckingEnabled=true]
|  created variable myFrame : JFrame
woman avatar