JShell Cheatsheet

1514




Jshell is Java’s first official REPL (READ-EVAL-PRINT-LOOP) tool which provides interactive use of Java Programming Language elements.

Variables

When you evaluate any valid java expression, the result will be stored in the system defined variables.

User-defined variables syntax:

datatype variable name = value;

Commands

  • To launch Jshell tool

    jshell
  • To exit Jshell

    exit
  • To list all the declared variables.

    /vars
    /vars <variableName>
    /vars <variableSnippetId>
    /vars -start
    /vars -all

    Example:

    jshell> /vars
    |       String name = "OneCompiler"
    |       int id = 1
  • To list all the declared Methods

    /methods
    /methods <methodName>
    /methods <methodSnippetId>
    /methods -start
    /methods -all

    Example:

    jshell> /methods
    |    double sum(int,int)
  • To displays classes, interfaces, and enums

    /types
    /types <typeName>
    /types <typeSnippetId>
    /types -start
    /types -all
  • To see a list all the snippets

    /list
    /list -all
    /list -start
    /list <snippetName>
    /list <snippetId>
  • To save your snippets

    /save
  • To personalize your startup entries

    /set start
  • To get help about the list of the JShell commands.

    /help
  • To edit code of a method

    /edit method-name
  • To load Code from External Java File into REPL:

    /open filename.java

    You can also provide directory information along wwith filename.java if the file is present in different directory like /open c:\\Documents\\filename.java

  • To Drop a Snippet

    /drop <snippetName>
    /drop <snippetId>