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

JavaObject Oriented ProgrammingProgramming

<p><strong>JShell&nbsp;</strong>is a command-line tool introduced in <strong>Java 9</strong> that evaluates declarations, statements, and expressions without the <strong>main()</strong> method. JShell can set up a text editor called <strong>JShell Edit Pad</strong>, which allows us to modify the code very easily, and it can be launched using the &quot;<strong>/edit</strong>&quot; command.</p><p>Below are the different &quot;<strong>/edit</strong>&quot; commands used in Jshell.</p><pre class="prettyprint notranslate"><strong>/edit /edit [ID] /edit [Code_Name]</strong></pre><ul class="list"><li><strong>/edit:</strong> This command can be used without an argument, the &quot;/edit&quot; command displays all the active code in the text editor.</li><li><strong>/edit [ID]:</strong> This command displays in the text editor the code corresponding to the ID entered.</li><li><strong>/edit</strong> <strong>[Code_Name]:</strong> This comamnd displays in the text editor the code corresponding to the name entered.</li></ul><pre class="prettyprint notranslate" style=""><strong>jshell&gt; int i = 10 i ==&gt; 10 jshell&gt; double j = 20.0 j ==&gt; 20.0 jshell&gt; public int sum(int x, int y) { ...&gt; return x + y; ...&gt; } | created method sum(int,int) jshell&gt; /edit</strong></pre><p>In the above, we create integer i, double y along with a method called &ldquo;<strong>sum</strong>&rdquo;. By entering the &quot;<strong>/edit</strong>&quot; command, it displays &quot;<strong>JShell Edit Pad</strong>&quot; as below.</p><p><img src="https://www.tutorialspoint.com/assets/questions/media/37354/jshell_edit1.PNG" class="fr-fic fr-dib" style="width:527px;height:277px" width="527" height="277"></p><p>We can use the editor to <strong>add&nbsp;</strong>or <strong>modify&nbsp;</strong>code and validate the set with the <strong>Accept&nbsp;</strong>button. We can add a new <strong>divide()</strong> method and modify the value of the variable <strong>&quot;y&quot;</strong> as below</p><p><img src="https://www.tutorialspoint.com/assets/questions/media/37354/jshell_edit2.PNG" class="fr-fic fr-dib" style="width:527px;height:273px" width="527" height="273"></p><pre class="prettyprint notranslate" style=""><strong>jshell&gt; int i = 10 i ==&gt; 10 jshell&gt; double j = 20 j ==&gt; 20.0 jshell&gt; public int sum(int x, int y) { ...&gt; return x + y; ...&gt; } | created method sum(int,int) jshell&gt; /edit j ==&gt; 50.0 | created method divide(double,double)</strong></pre><p style="">If we want to display only the variable &quot;<strong>i</strong>&quot; in <strong>JShell Edit Pad,</strong> just type the command &quot;<strong>/edit i</strong>&quot;, and it displays 10. In the same way, if we want to modify only the text of &quot;<strong>ID 2&quot;</strong> (corresponding to the variable y), just enter the command &quot;<strong>/edit 2</strong>&quot;, and it displays 20.</p>
raja
Updated on 14-Apr-2020 18:13:27

Advertisements