Saturday 14 January 2017

Jshell: java9 Interpreter

Java9 come up with new interactive shell called jshell, it is a simple, interactive computer programming environment that takes single user inputs evaluates them, and returns the result to the user.  It is very useful to the new comers of Java programming environment.

Jshell is a command line interpreter, it is presented in bin directory of java9 installation.

How to open jshell?
Open command prompt and type ‘jshell’.
C:\Users\Krishna>jshell
|  Welcome to JShell -- Version 9-ea
|  For an introduction type: /help intro

jshell>

Get the help of all the commands available in jshell
Type /help in the jshell terminal, it gives list of the commands that are available in jshell.
jshell> /help
|  Type a Java language expression, statement, or declaration.
|  Or type one of the following commands:
|
|  /list [<name or id>|-all|-start]
|       list the source you have typed
|  /edit <name or id>
|       edit a source entry referenced by name or id
|  /drop <name or id>
|       delete a source entry referenced by name or id
|  /save [-all|-history|-start] <file>
|       Save snippet source to a file.
|  /open <file>
|       open a file as source input
|  /vars [<name or id>|-all|-start]
|       list the declared variables and their values
|  /methods [<name or id>|-all|-start]
|       list the declared methods and their signatures
|  /types [<name or id>|-all|-start]
|       list the declared types
|  /imports
|       list the imported items
|  /exit
|       exit jshell
|  /reset
|       reset jshell
|  /reload [-restore] [-quiet]
|       reset and replay relevant history -- current or previous (-restore)
|  /classpath <path>
|       add a path to the classpath
|  /history
|       history of what you have typed
|  /help [<command>|<subject>]
|       get information about jshell
|  /set editor|start|feedback|mode|prompt|truncation|format ...
|       set jshell configuration information
|  /? [<command>|<subject>]
|       get information about jshell
|  /!
|       re-run last snippet
|  /<id>
|       re-run snippet by id
|  /-<n>
|       re-run n-th previous snippet
|
|  For more information type '/help' followed by the name of a
|  command or a subject.
|  For example '/help /list' or '/help intro'.  Subjects:
|
|  intro
|       an introduction to the jshell tool
|  shortcuts
|       a description of shortcuts



Evaluating Expressions
You can evaluate any java valid expressions in jshell.
jshell> 10 + 20
$1 ==> 30

jshell> 10 - 20
$2 ==> -10

jshell> true && false
$3 ==> false

jshell> 2 >> 5
$4 ==> 0

jshell> 2 << 5
$5 ==> 64

Defining variables in jshell
You can define variables and use them.
jshell> int principle = 10000;
principle ==> 10000

jshell> int time = 5;
time ==> 5

jshell> double rateOfIntrest = 1.5;
rateOfIntrest ==> 1.5

jshell> double intrest = (principle * time * rateOfIntrest)/100
intrest ==> 750.0

Print the value of variable
Just type the variable name and press enter, you can see the value associated with the variable.
jshell> int counter = 10
counter ==> 10

jshell> counter
counter ==> 10

Notify above statements, ‘;’ is not compulsory at the end of the statements.

Defining Methods
You can define the methods in jshell and use them.

Example
int fact(int n) {if(n==0) return 1; return n * fact(n-1);}

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

jshell> fact(10)
$14 ==> 3628800

jshell> fact(5)
$15 ==> 120

It is not necessary to define the methods in same line, you can define the method across multiple lines.
jshell> int fact(int n){
   ...>    if(n == 0){
   ...>       return 1;
   ...>    }
   ...>    return n * fact(n-1);
   ...> }
|  modified method fact(int)

As you see the output, it is returning ‘modified method fact(int)’, that means you can redefine already defined methods.
jshell> fact(5)
$17 ==> 120

jshell> fact(6)
$18 ==> 720

Jshell Commands summary

/imports : list the imported items
jshell> /imports
|    import java.io.*
|    import java.math.*
|    import java.net.*
|    import java.nio.file.*
|    import java.util.*
|    import java.util.concurrent.*
|    import java.util.function.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import java.util.stream.*

/list : list the source you have typed
jshell> /list

   1 : 10 + 20
   2 : 10 - 20
   3 : true && false
   4 : 2 >> 5
   5 : 2 << 5
   6 : int principle = 10000;
   7 : int time = 5;
   8 : double rateOfIntrest = 1.5;
   9 : double intrest = (principle * time * rateOfIntrest)/100;
  10 : intrest
  11 : int counter = 10;
  12 : counter
  14 : fact(10)
  15 : fact(5)
  16 : int fact(int n){
          if(n == 0){
             return 1;
          }
          return n * fact(n-1);
       }
  17 : fact(5)
  18 : fact(6)

/edit <name or id>: Edit a source entry referenced by name or id in the jshell Edit Pad.
Type ‘/edit fact’ in the jshell terminal, it opens the fact method definition in jshell edit pad.
jshell> /edit fact


/drop <name or id> : delete a source entry referenced by name or id
Get all the sources that you typed using /list command.
jshell> /list

   1 : 10 + 20
   2 : 10 - 20
   3 : true && false
   4 : 2 >> 5
   5 : 2 << 5
   6 : int principle = 10000;
   7 : int time = 5;
   8 : double rateOfIntrest = 1.5;
   9 : double intrest = (principle * time * rateOfIntrest)/100;
  10 : intrest
  11 : int counter = 10;
  12 : counter
  14 : fact(10)
  15 : fact(5)
  16 : int fact(int n){
          if(n == 0){
             return 1;
          }
          return n * fact(n-1);
       }
  17 : fact(5)
  18 : fact(6)

Remove the variable counter.
jshell> /drop counter
|  dropped variable counter

Now get all the sources that you typed using /list command.
jshell> /list

   1 : 10 + 20
   2 : 10 - 20
   3 : true && false
   4 : 2 >> 5
   5 : 2 << 5
   6 : int principle = 10000;
   7 : int time = 5;
   8 : double rateOfIntrest = 1.5;
   9 : double intrest = (principle * time * rateOfIntrest)/100;
  10 : intrest
  12 : counter
  14 : fact(10)
  15 : fact(5)
  16 : int fact(int n){
          if(n == 0){
             return 1;
          }
          return n * fact(n-1);
       }
  17 : fact(5)
  18 : fact(6)

Notify above snippet, the variable ‘counter’ is removed.

/save [-all|-history|-start] <file> : Save snippet source to a file.
jshell> /save -all commands.txt

/open <file> : open a file as source input
jshell> /open commands.txt

/vars [<name or id>|-all|-start]: list the declared variables and their values
jshell> /vars
|    int $1 = 30
|    int $2 = -10
|    boolean $3 = false
|    int $4 = 0
|    int $5 = 64
|    int $14 = 3628800
|    int $15 = 120
|    int $17 = 120
|    int $18 = 720
|    int $29 = 30
|    int $30 = -10
|    boolean $31 = false
|    int $32 = 0
|    int $33 = 64
|    int principle = 10000
|    int time = 5
|    double rateOfIntrest = 1.5
|    double intrest = 750.0
|    int counter = 10
|    int $42 = 3628800
|    int $43 = 120
|    int $45 = 120
|    int $46 = 720

jshell> /vars principle
|    int principle = 10000

jshell> /vars 15
|    int $15 = 120

/methods [<name or id>|-all|-start] : list the declared methods and their signatures
jshell> /methods
|    fact (int)int


/types [<name or id>|-all|-start]list the declared types
jshell> class Box{}
|  created class Box

jshell> class Employee{ String firstName; String lastName;}
|  created class Employee

jshell> /types
|    class Box
|    class Employee


/history : history of what you have typed
jshell> /history

/help
10 = 20
10 + 20
10 - 20
true && false
2 >> 5
2 << 5
int principle = 10000;
int time = 5;
int rateOfIntrest = 1.5;
double rateOfIntrest = 1.5;
double intrest = (principle * time * rateOfIntrest)/100
intrest
int counter = 10
counter
int fact(n) = {if(n==0) return 1; return n * fact(n-1)}
int fact(n) = {if(n==0) return 1; return n * fact(n-1)}
int fact(n){
if(n == 0){
int fact(n) {if(n==0) return 1; return n * fact(n-1)}
int fact(int n) {if(n==0) return 1; return n * fact(n-1)}
int fact(int n) {if(n==0) return 1; return n * fact(n-1);}
fact(10)
fact(5)
int fact(int n){
   if(n == 0){
      return 1;
   }
   return n * fact(n-1);
}
fact(5)
fact(6)
/imports
/list
/edti fact
/edit fact
/edit fact
/list
/drop counter
/list
/save -all commands.txt
/open commands.txt
/vars
/vars principle
/vars 15
/methods
/types
class Box{}
class Employee{ String firstName; String lastName;}
/types
/history

/? [<command>|<subject>] : Get information about jshell commands
jshell> /? /list
|
|  /list
|
|  Show the source of snippets, prefaced with the snippet id.
|
|  /list
|       List the currently active snippets of code that you typed or read with /open
|
|  /list -start
|       List the automatically evaluated start-up snippets
|
|  /list -all
|       List all snippets including failed, overwritten, dropped, and start-up
|
|  /list <name>
|       List snippets with the specified name (preference for active snippets)
|
|  /list <id>
|       List the snippet with the specified snippet id

jshell> /? /vars
|
|  /vars
|
|  List the type, name, and value of jshell variables.
|
|  /vars
|       List the type, name, and value of the current active jshell variables
|
|  /vars <name>
|       List jshell variables with the specified name (preference for active variables)
|
|  /vars <id>
|       List the jshell variable with the specified snippet id
|
|  /vars -start
|       List the automatically added start-up jshell variables
|
|  /vars -all
|       List all jshell variables including failed, overwritten, dropped, and start-up

/! : re-run last snippet
jshell> /!
class Employee{ String firstName; String lastName;}
|  modified class Employee

/-<n> : re-run n-th previous snippet
jshell> /-4
class Employee{ String firstName; String lastName;}
|  modified class Employee

jshell> /-10
int fact(int n){
   if(n == 0){
      return 1;
   }
   return n * fact(n-1);
}
|  modified method fact(int)

/help /shortcuts : a description of shortcuts


/reset : reset jshell
jshell> /list

   1 : 10 + 20
   2 : 10 - 20
   3 : true && false
   4 : 2 >> 5
   5 : 2 << 5
  10 : intrest
  12 : counter
  14 : fact(10)
  15 : fact(5)
  17 : fact(5)
  18 : fact(6)
  19 : import java.io.*;
  20 : import java.math.*;
  21 : import java.net.*;
  22 : import java.nio.file.*;
  23 : import java.util.*;
  24 : import java.util.concurrent.*;
  25 : import java.util.function.*;
  26 : import java.util.prefs.*;
  27 : import java.util.regex.*;
  28 : import java.util.stream.*;
  29 : 10 + 20
  30 : 10 - 20
  31 : true && false
  32 : 2 >> 5
  33 : 2 << 5
  34 : int principle = 10000;
  35 : int time = 5;
  36 : double rateOfIntrest = 1.5;
  37 : double intrest = (principle * time * rateOfIntrest)/100;
  38 : intrest
  39 : int counter = 10;
  40 : counter
  42 : fact(10)
  43 : fact(5)
  45 : fact(5)
  46 : fact(6)
  47 : class Box{}
  54 : int fact(int n){
          if(n == 0){
             return 1;
          }
          return n * fact(n-1);
       }
  55 : class Employee{ String firstName; String lastName;}

Now reset the jshell, all the variable, methods that are defined previously are cleared.
jshell> /reset
|  Resetting state.

jshell> /list

jshell> /methods

jshell> /vars

/exit : Exit from jshell
jshell> /exit
|  Goodbye



Previous                                                 Next                                                 Home

No comments:

Post a Comment