Java 9 – The Java Shell (RPEL) Part 2 | Code Factory


Index Page : Link

Donate : Link

Medium Blog : Link

Applications : Link

5. Editing and Navigating Code Snippets

We can list all our active snippets with /list command and we can list total history of our jshell
activities with /history command.

jshell> /list

   1 : System.out.println("Code Factory")
   2 : int x=10;
   3 : 10+20

jshell> /history

System.out.println("Code Factory")
int x=10
10+20
  1. By using down arrow and up arrow we can navigate through history.While navigating we can use left and right arrows to move character by character with in the snippet.
  2. We can Ctrl+A to move to the beginning of the line and Ctrl+E to move to the end of the line.
  3. We can use Alt+B to move backward by one word and Alt+F to move forward by one word.
  4. We can use Delete key to delete the character at the cursor. We can us Backspace to delete character before the cursor.
  5. We can use Ctrl+K to delete the text from the cursor to the end of line.
  6. We can use Alt+D to delete the text from the cursor to the end of the word.
  7. Ctrl+W to delete the text from cursor to the previous white space.
  8. Ctrl+Y to paste most recently deleted text into the line.
  9. Ctrl+R to Search backward through history
  10. Ctrl+S to search forward through histroy
KEYACTION
Up arrowMoves up one line, backward through history
Down arrowMoves down one line, forward through history
Left arrowMoves backward one character
Right arrowMoves forward one character
Ctrl+AMoves to the beginning of the line
Ctrl+EMoves to the end of the line
Alt+BMoves backward one word
Alt+FMoves forward one word
DeleteDeletes the character at the cursor
BackspaceDeletes the character before the cursor
Ctrl+KDeletes the text from the cursor to the end of the line
Alt+DDeletes the text from the cursor to the end of the word
Ctrl+WDeletes the text from the cursor to the previous white space.
Ctrl+YPastes the most recently deleted text into the line.
Ctrl+RSearches backward through history
Ctrl+SSearches forwards through history

6. Working with JShell Variables

In JShell,there are 2 types of variables

  1. Explicit variables
  2. Implicit variables or Scratch variables

Explicit variables:

These variables created by programmer explicitly based on our programming requirement.

e.g.

jshell> int a=34
a ==> 34
|  created variable a : int

jshell> String s="Code Factory"
s ==> "Code Factory"
|  created variable s : String

The variables x and s provided explicitly by the programmer and hence these are explicit variables.

Implicit Variables:

Sometimes JShell itself creates variables implicitly to hold temporary values,such type of variables
are called Implicit variables.

jshell> 3+4
$3 ==> 7
|  created scratch variable $3 : int

jshell> 3<4
$4 ==> true
|  created scratch variable $4 : boolean

The variables $3 and $4 are created by JShell and hence these are implicit variables.

Based on requirement we can use these scratch variables also.

jshell> $3+3
$5 ==> 10
|  created scratch variable $5 : int

If we are trying to declare a variable with the same name which is already available then old
variable will be replaced with new variable. i.e in JShell, variable overriding is possible.

In JShell at a time only one variable is possible with the same name. i.e 2 variables with the same
name is not allowed.

jshell> String a="A"
a ==> "A"
|  replaced variable a : String
|    update overwrote variable a : int

In the above case, int variable a is replaced with String variable a.

While declaring variables compulsory the types must be matched, otherwise we will get compile
time error.

jshell> int i=false
|  Error:
|  incompatible types: boolean cannot be converted to int
|  int i=false;
|        ^---^

Note: By using /vars command we can list out type,name and value of all variables which are created in JShell.
Instead of /vars we can also use /var,/va,/v

jshell> /help vars
|
|                                   /vars
|                                   =====
|
|  List the type, name, and value of variables that were entered.
|
|  /vars
|       List the type, name, and value of the current active variables
|
|  /vars <name>
|       List variables with the specified name (preference for active variables)
|
|  /vars <id>
|       List the variable with the specified snippet ID.
|       One or more IDs or ID ranges may used, see '/help id'
|
|  /vars -start
|       List the variables in the evaluated startup snippets
|
|  /vars -all
|       List all variables including failed, overwritten, dropped, and startup

To List out All Active variables of JShell:

jshell> /vars
|    String s = "Code Factory"
|    int $3 = 7
|    boolean $4 = true
|    int $5 = 10
|    String x = "X"
|    String a = "A"

To List out All Variables(both active and not-active):

jshell> /vars -all
|    int a = (not-active)
|    String s = "Code Factory"
|    int $3 = 7
|    boolean $4 = true
|    int $5 = 10
|    String x = "X"
|    String a = "A"
|    int i = (not-active)

We can drop a variable by using /drop command

jshell> /vars
|    String s = "Code Factory"
|    int $3 = 7
|    boolean $4 = true
|    int $5 = 10
|    String x = "X"
|    String a = "A"

jshell> /drop $3
|  dropped variable $3

jshell> /vars
|    String s = "Code Factory"
|    boolean $4 = true
|    int $5 = 10
|    String x = "X"
|    String a = "A"

We can create complex variables also

jshell> List<String> strs = List.of("Aa", "Bb", "Cc");
strs ==> [Aa, Bb, Cc]
|  created variable strs : List<String>

jshell> List<String> list = List.of("Xx", "Yy", "Zz");
list ==> [Xx, Yy, Zz]
|  created variable list : List<String>

jshell> List<List<String>> lists = List.of(strs, list);
lists ==> [[Aa, Bb, Cc], [Xx, Yy, Zz]]
|  created variable lists : List<List<String>>

jshell> /vars
|    String s = "Code Factory"
|    boolean $4 = true
|    int $5 = 10
|    String x = "X"
|    String a = "A"
|    List<String> strs = [Aa, Bb, Cc]
|    List<String> list = [Xx, Yy, Zz]
|    List<List<String>> lists = [[Aa, Bb, Cc], [Xx, Yy, Zz]]

System.out.println() vs System.out.printf() methods:

public class PrintStream
{
public void print(boolean);
public void print(char);
 public void println(boolean);
public void println(char);
 public PrintStream printf(String,Object...);
....
}

System.out.println() method return type is void.

But System.out.printf() method return type is PrintStread object. On that PrintStream object we can call printf() method again.

jshell> System.out.println("Code");
Code

jshell> System.out.printf("Code %s", "Factory");
Code Factory$12 ==> java.io.PrintStream@396a51ab
|  created scratch variable $12 : PrintStream

jshell> $12.printf("World");
World$13 ==> java.io.PrintStream@396a51ab
|  created scratch variable $13 : PrintStream

jshell> /vars
|    String s = "Code Factory"
|    boolean $4 = true
|    int $5 = 10
|    String x = "X"
|    String a = "A"
|    List<String> strs = [Aa, Bb, Cc]
|    List<String> list = [Xx, Yy, Zz]
|    List<List<String>> lists = [[Aa, Bb, Cc], [Xx, Yy, Zz]]
|    PrintStream $12 = java.io.PrintStream@396a51ab
|    PrintStream $13 = java.io.PrintStream@396a51ab

7. Working with JShell Methods

In the JShell we can create our own methods and we can invoke these methods multiple times based on our requirement.

jshell> public void m1()
   ...> {
   ...> System.out.println("m1");
   ...> }
|  created method m1()

jshell> m1()
m1

jshell> public void m2()
   ...> {
   ...> System.out.println("m2() method");
   ...> }
|  created method m2()

jshell> m2()
m2() method

In the JShell there may be a chance of multiple methods with the same name but different argument types, and such type of methods are called overloaded methods.Hence we can declare oveloaded methods in the JShell.

jshell> public void m3(){}
|  created method m3()

jshell> public void m3(int i){}
|  created method m3(int)

jshell> public void m3(String s){}
|  created method m3(String)

jshell> /methods
|    void m1()
|    void m2()
|    void m3()
|    void m3(int)
|    void m3(String)

We can list out all methods information by using /methods command.

jshell> /help methods
|
|                                  /methods
|                                  ========
|
|  List the name, parameter types, and return type of methods that were entered.
|
|  /methods
|       List the name, parameter types, and return type of the current active methods
|
|  /methods <name>
|       List methods with the specified name (preference for active methods)
|
|  /methods <id>
|       List the method with the specified snippet ID.
|       One or more IDs or ID ranges may used, see '/help id'
|
|  /methods -start
|       List the methods in the evaluated startup snippets
|
|  /methods -all
|       List all snippets including failed, overwritten, dropped, and startup

jshell> /methods
|    void m1()
|    void m2()
|    void m3()
|    void m3(int)
|    void m3(String)

If we are trying to declare a method with same signature of already existing method in JShell,then old method will be overridden with new method(eventhough return types are different). i.e in JShell at a time only one method with same signature is possible.

jshell> public void m1(int i){}
|  created method m1(int)

jshell> public int m1(int i){return 34;}
|  replaced method m1(int)
|    update overwrote method m1(int)

jshell> /methods
|    int m1(int)

jshell> /methods -all
|    void m1(int)
|    int m1(int)

In the JShell we can create more complex methods also.

jshell> public void sum(int... x)
   ...> {
   ...> int total=0;
   ...> for(int i:x)
   ...> {
   ...> total = total+i;
   ...> }
   ...> System.out.println("Total : " + total);
   ...> }
|  created method sum(int...)


jshell> sum(1,2,3,4)
Total : 10

jshell> sum(10,20,30,40)
Total : 100

In JShell, inside method body we can use undeclared variables and methods. But until declaring all
dependent variables and methods, we cannot invoke that method.

Usage of undeclared variable inside method body

jshell> public void m1()
   ...> {
   ...> System.out.println(x);
   ...> }
|  created method m1(), however, it cannot be invoked until variable x is declared

jshell> m1()
|  attempted to call method m1() which cannot be invoked until variable x is declared

jshell> int x=34
x ==> 34
|  created variable x : int
|    update modified method m1()

jshell> m1()
34

Usage of undeclared method inside method body

jshell> public void m1()
   ...> {
   ...> m2();
   ...> }
|  created method m1(), however, it cannot be invoked until method m2() is declared

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

jshell> public void m2()
   ...> {
   ...> System.out.println("Code Factory");
   ...> }
|  created method m2()
|    update modified method m1()

jshell> m1()
Code Factory

jshell> m2()
Code Factory

We can drop methods by name with /drop command. If multiple methods with the same name then we should drop by snippet id. If multiple methods with the same name then it will drop all the methods.

jshell> public void m1(){}
|  created method m1()

jshell> public void m1(int i){}
|  created method m1(int)

jshell> public void m2(){}
|  created method m2()

jshell> /methods
|    void m1()
|    void m1(int)
|    void m2()

jshell> /drop m2
|  dropped method m2()

jshell> /methods
|    void m1()
|    void m1(int)

jshell> /drop m1
|  dropped method m1()
|  dropped method m1(int)

jshell> /methods

jshell> public void m1(){}
|  created method m1()

jshell> public void m2(int i){}
|  created method m2(int)

jshell> /methods
|    void m1()
|    void m2(int)

jshell> /list

   1 : public void m1(){}
   2 : public void m2(int i){}

jshell> /drop 2
|  dropped method m2(int)

jshell> /methods
|    void m1()

8. Using An External Editor with JShell

It is very difficult to type lengthy code from JShell. To overcome this problem, JShell provide inbuilt editor.

We can open inbuilt editor with the command: /edit

jshell> /edit

If we are not satisfied with JShell in-built editor ,then we can set our own editor to the JShell. For this we have to use /set editor command.

jshell> /set editor "C:\\Windows\\System32\\notepad.exe"
|  Editor set to: C:\Windows\System32\notepad.exe

jshell> /set editor "C:\\Program Files (x86)\\Notepad++\\notepad++.exe"
|  Editor set to: C:\Program Files (x86)\Notepad++\notepad++.exe

Now If we type /edit automatically new editor will be openend.

But this way of setting editor is temporary and it is applicable only for current session.

If we want to set current editor as permanent, then we have to use the command

jshell> /set editor -retain
|  Editor setting retained: C:\Program Files (x86)\Notepad++\notepad++.exe

How to set default editor once again:

We have to type the following command from the jshell

jshell> /set editor -default
|  Editor set to: -default

To make default editor as permanent:

jshell> /set editor -retain
|  Editor setting retained: -default

Leave a comment