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

JavaObject Oriented ProgrammingProgramming

<p style=""><strong>JShell&nbsp;</strong>tool has introduced in <strong>Java 9&nbsp;</strong>version. It is also called a <strong>REPL</strong>(Read-Evaluate-Print-Loop) tool that allows us to execute Java code and getting immediate results. We need to list out the declared types like <strong>class</strong>, <strong>interface</strong>, <strong>enum</strong>, and etc by using the &quot;<strong>/types</strong>&quot; command.</p><p style="">Below are the different &quot;<strong>/types</strong>&quot; commands in JShell.</p><pre class="prettyprint notranslate"><strong>/types /types [ID] /types [Type_Name] /types -start /types -all</strong></pre><ul class="list"><li><strong>/types:</strong> This command lists all active types (class, interface, enum) created in JShell.</li><li><strong>/types [ID]:</strong> This command displays the type corresponding to the id <strong>[ID]</strong>.</li><li><strong>/types [Type_Name]:</strong> This command displays the type corresponding to <strong>[Type_Name]</strong>.</li><li><strong>/types -start:</strong> This command allows us to list the types that have been added to the JShell startup script</li><li><strong>/types -all:</strong> This command allows us to list all types of the current session (active, inactive and loaded when JShell starts).</li></ul><p style="">In the below code snippet, created class, interface, and enum types. Then, we can apply different &quot;<strong>/types</strong>&quot; commands.&nbsp;</p><pre class="prettyprint notranslate" style=""><strong>jshell&gt; enum Operation { ...&gt; &nbsp; &nbsp; &nbsp; ADDITION, ...&gt; &nbsp; &nbsp; &nbsp; DIVISION; ...&gt; &nbsp; &nbsp;} | created enum Operation jshell&gt; class Employee { ...&gt; &nbsp; &nbsp; &nbsp; String empName; ...&gt; &nbsp; &nbsp; &nbsp; int age; ...&gt; &nbsp; &nbsp; &nbsp; public void empData() { ...&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(&quot;Employee Name is: &quot; + empName); ...&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(&quot;Employee Age is: &quot; + age); ...&gt; &nbsp; &nbsp; &nbsp; } ...&gt; &nbsp; &nbsp;} | created class Employee jshell&gt; interface TestInterface { ...&gt; &nbsp; &nbsp; &nbsp; public void sum(); ...&gt; &nbsp; &nbsp;} | created interface TestInterface jshell&gt; /types | enum Operation | class Employee | interface TestInterface jshell&gt; /types 1 | enum Operation jshell&gt; /types -start jshell&gt; /drop Operation | dropped enum Operation jshell&gt; /types -all | enum Operation | class Employee | interface TestInterface</strong></pre>
raja
Updated on 09-Apr-2020 14:57:27

Advertisements