Different methods for executing a JShell command in Java

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


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.