Java-9 JShell (REPL)





Java Shell (JShell) ?

  • JShell is an interactive tool to execute and evaluate java simple programs like variable declarations, statements, expressions, simple Programs etc.

  • JShell is a REPL (Read Evaluate Print Loop) tool and run from the command line.

  • It allows us to execute Java code from the shell and shows output immediately.


How to start JShell ?

To start Jshell, first you must have installed Java 9 then open terminal in Linux or command prompt in windows and type jshell. It will start jshell session.



C:\users\prowessapps>jshell 
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro

jshell>
                

Hello World message

To display a simple "Hello World" message, use print command without creating class and hit enter.



jshell> System.out.println("Hello World")
Hello World

jshell> 
                

In the first time of Java history, here you can observe that semicolon ; is optional.

Here just look at the code snippets, going to use in REPL


 Variables
jshell> int a = 20
a ==> 10

jshell> System.out.println("a = "+a)
a = 10

---------------------
 Scratch Variables
If you do not provide variable name,Java create implicit variable to store the value.
These variables start with $ sign.

jshell> 20
$5 ==> 20

jshell> System.out.println($5)
20

---------------------
 Expressions
You can test any valid Java expression to get instant output
.
jshell> 2+3
$6 ==> 5

---------------------
 Methods
To test method business logic, create an method and get result immediately.

jshell> void display(){
   ...> System.out.println("This is display method");
   ...> }
|  created method display()

//calling method
jshell> display()
This is display method

---------------------
 Class
jshell>class Test{
   ...> void show(){
   ...> System.out.println("This is show method");
   ...> }  
   ...> }
|  created class Test

jshell> new Test().show();  
This is show method 

                

Package Imports

By default, 10 packages are imported and you can also import any package by using import statement.

To see, default import packages, you can use following command.

jshell> /import
|    import java.io.*
|    import java.math.*
|    import java.net.*
|    import java.nio.file.*
|    import java.util.*
|    import java.util.concurrent.*
|    import java.util.function.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import java.util.stream.*

Importing java.sql package.

jshell> import java.sql.*;

jshell> /import
|    import java.io.*
|    import java.math.*
|    import java.net.*
|    import java.nio.file.*
|    import java.util.*
|    import java.util.concurrent.*
|    import java.util.function.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import java.util.stream.*
|    import java.sql.*

Now number of packages are 11 including java.sql.*.
                

Jshell Commands

Type /help once jshell command starts running.

Jshell provides numerous useful commands that you can use to modify environment, manage code and to get code related information.

jshell> /help
|  Type a Java language expression, statement, or declaration.
|  Or type one of the following commands:
|  /list [<name or id>|-all|-start]
|       list the source you have typed
|  /edit <name or id>
|       edit a source entry referenced by name or id
|  /drop <name or id>
|       delete a source entry referenced by name or id
|  /save [-all|-history|-start] <file>
|       Save snippet source to a file.
|  /open <file>
|       open a file as source input
|  /vars [<name or id>|-all|-start]
|       list the declared variables and their values
|  /methods [<name or id>|-all|-start]
|       list the declared methods and their signatures
|  /types [<name or id>|-all|-start]
|       list the declared types
|  /imports
|       list the imported items
|  /exit
|       exit jshell
|  /env [-class-path <path>] [-module-path <path>] [-add-modules <modules>] ...
|       view or change the evaluation context
|  /reset [-class-path <path>] [-module-path <path>] [-add-modules <modules>]...

|       reset jshell
|  /reload [-restore] [-quiet] [-class-path <path>] [-module-path <path>]...
|       reset and replay relevant history -- current or previous (-restore)
|  /history
|       history of what you have typed
|  /help [<command>|<subject>]
|       get information about jshell
|  /set editor|start|feedback|mode|prompt|truncation|format ...
|       set jshell configuration information
|  /? [<command>|<subject>]
|       get information about jshell
|  /!
|       re-run last snippet
|  /<id>
|       re-run snippet by id
|  /-<n>
|       re-run n-th previous snippet
|
|  For more information type '/help' followed by the name of a
|  command or a subject.
|  For example '/help /list' or '/help intro'.
|
|  Subjects:
|
|  intro
|       an introduction to the jshell tool
|  shortcuts
|       a description of keystrokes for snippet and command completion,
|       information access, and automatic code generation
|  context
|       the evaluation context options for /env /reload and /reset
                    
                

Package Imports

jshell> /import 
|    import java.io.*
|    import java.math.*
|    import java.net.*
|    import java.nio.file.*
|    import java.util.*
|    import java.util.concurrent.*
|    import java.util.function.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import java.util.stream.*
|    import java.sql.*
                

Command /vars to show variables

jshell> /vars
|    int $1 = 1
|    int $2 = 3
|    int $4 = 24
          

/list : to show all written source codes

jshell> /list

 1 : 2+3  
 2 : 10-5  
 3 : $1  
 4 : 2+(5*4+(2+1))  
 5 : void display(){  
     System.out.println("This is display method");
     }  
 6 : display();  
 7 : class Test{  
     void show(){  
     System.out.println("This is show method");
     }  
     }  
 8 : new Test().show();  
 9 : import java.sql.*;
            

Exiting from jshell

jshell> /exit
| Goodbye
                

Next topic is The Java Platform module system (Project Jigsaw)




 






Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in



CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved