Skip to content
Related Articles

Related Articles

Improve Article

Different methods for executing a JShell command in Java

  • Last Updated : 16 Apr, 2020

Jshell is an interactive Java Shell tool, it allows us to execute Java code from the shell and shows output immediately. JShell is a REPL (Read Evaluate Print Loop) tool and run from the command line.

Different methods for executing a JShell command:

  1. jshell: In this only output come without any description of it. It uses only System.out.println(…) method to print.
    Example:
    C:\Windows\SysWOW64>jshell
    | Welcome to JShell -- Version 13.0.1
    | For an introduction type: /help intro
    
    jshell> int i=10;
    i ==> 10
    
    jshell> int j=20;
    j ==> 20
    
    jshell> int t=i+j;
    t ==> 30
    
    jshell> System.out.println((t+5));
    35
    

  2. jshell -v: Jshell -v is a type of Java shell in which a little description shows after the user gets output. It uses only the System.out.println(…) method to print.
    Example:
    C:\Windows\SysWOW64>jshell -v
    |  Welcome to JShell -- Version 13.0.1
    |  For an introduction type: /help intro
    
    jshell> int i=10;
    i ==> 10
    |  created variable i : int
    
    jshell> int j=20;
    j ==> 20
    |  created variable j : int
    
    jshell> int t=i+j;
    t ==> 30
    |  created variable t : int
    
    jshell> System.out.println((t+5));
    35
    



  3. jshell PRINTING: Jshell PRINTING is another type of Java shell tool in which print method is provided (which is not present in jshell and jshell -v). It uses print(…) and System.out.println(…) method to print.
    Example:
    C:\Windows\SysWOW64>jshell PRINTING
    |  Welcome to JShell -- Version 13.0.1
    |  For an introduction type: /help intro
    
    jshell> int i=4;
    i ==> 4
    
    jshell> int j=9;
    j ==> 9
    
    jshell> int t=i+j;
    t ==> 13
    
    jshell> print(t);
    13
    

    If we use the print(..) method in jshell and jshell -v then an error will occur.
    Example:

    C:\Windows\SysWOW64>jshell
    |  Welcome to JShell -- Version 13.0.1
    |  For an introduction type: /help intro
    
    jshell> int i=4;
    i ==> 4
    
    jshell> int j=9;
    j ==> 9
    
    jshell> int t=i+j;
    t ==> 13
    
    jshell> print(t);
    |  Error:
    |  cannot find symbol
    |    symbol:   method print(int)
    |  print(t);
    |  ^---^
    

    C:\Windows\SysWOW64>jshell -v
    |  Welcome to JShell -- Version 13.0.1
    |  For an introduction type: /help intro
    
    jshell> int i=4;
    i ==> 4
    |  created variable i : int
    
    jshell> int j=9;
    j ==> 9
    |  created variable j : int
    
    jshell> int t=i+j;
    t ==> 13
    |  created variable t : int
    
    jshell> print(t);
    |  Error:
    |  cannot find symbol
    |    symbol:   method print(int)
    |  print(t);
    |  ^---^
    

My Personal Notes arrow_drop_up
Recommended Articles
Page :