What are the different "/edit" commands in JShell in Java 9?

JavaObject Oriented ProgrammingProgramming

JShell is a command-line tool introduced in Java 9 that evaluates declarations, statements, and expressions without the main() method. JShell can set up a text editor called JShell Edit Pad, which allows us to modify the code very easily, and it can be launched using the "/edit" command.

Below are the different "/edit" commands used in Jshell.

/edit
/edit [ID]
/edit [Code_Name]
  • /edit: This command can be used without an argument, the "/edit" command displays all the active code in the text editor.
  • /edit [ID]: This command displays in the text editor the code corresponding to the ID entered.
  • /edit [Code_Name]: This comamnd displays in the text editor the code corresponding to the name entered.
jshell> int i = 10
i ==> 10

jshell> double j = 20.0
j ==> 20.0

jshell> public int sum(int x, int y) {
...>       return x + y;
...>    }
| created method sum(int,int)

jshell> /edit

In the above, we create integer i, double y along with a method called “sum”. By entering the "/edit" command, it displays "JShell Edit Pad" as below.

We can use the editor to add or modify code and validate the set with the Accept button. We can add a new divide() method and modify the value of the variable "y" as below

jshell> int i = 10
i ==> 10

jshell> double j = 20
j ==> 20.0

jshell> public int sum(int x, int y) {
...>       return x + y;
...>    }
| created method sum(int,int)

jshell> /edit
j ==> 50.0
| created method divide(double,double)

If we want to display only the variable "i" in JShell Edit Pad, just type the command "/edit i", and it displays 10. In the same way, if we want to modify only the text of "ID 2" (corresponding to the variable y), just enter the command "/edit 2", and it displays 20.

raja
Published on 14-Apr-2020 18:13:27
Advertisements