Java9 JShell
Java9 JShell
Java9 JShell
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
1 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UNIT 1: Introduction to the JShell
Jshell is also known as Interactive console.
JShell is Java's own REPL Tool.
By using this tool we can execute java code snippets and we can get immediate results.
For beginners it is very good to start programming in fun way.
By using this jshell we can test and execute java expressions,statements,methods,classes etc.It is
useful for testing small code snippets very quickly,which can be plugged into our main coding
based on our requirement.
It is not new thing in Java. It is already there in other languages like Python,Swift,Lisp,Scala,Ruby
etc..
Python IDLE
Apple's Swift Programming Language PlayGround
Limitiations of JShell:
1. JShell is not meant for Main Coding.We can use just to test small coding snippets,which can be
used in our Main Coding.
3. It is not that much impressed feature. All other languages like Python,LISP,Scala,Ruby,Swift etc
are already having this REPL tools
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
2 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UNIT-2: Getting Started with JShell
Starting and Stopping JShell:
Open the jshell from the command prompt in verbose mode
jshell -v
D:\durga_classes>jshell -v
| Welcome to JShell -- Version 9
| For an introduction type: /help intro
Note: Observe the difference b/w with -v and without -v (verbose mode)
D:\durga_classes>jshell -v
| Welcome to JShell -- Version 9
| For an introduction type: /help intro
Note: If any information displaying on the jshell starts with '|', it is the information to the
programmer from the jshell
jshell> 10+20
$1 ==> 30
| created scratch variable $1 : int
jshell> 20-30*6/2
$2 ==> -70
| created scratch variable $2 : int
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
3 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
jshell> System.out.println("DURGASOFT")
DURGASOFT
Here if we observe the output not starts with | b'z it is not information from the Jshell.
Note: Terminating semicolons are automatically added to the end of complete snippet by JShell if
not entered. .
jshell> Math.sqrt(4)
$4 ==> 2.0
| created scratch variable $4 : double
jshell> Math.max(10,20)
$5 ==> 20
| created scratch variable $5 : int
jshell> Math.random()
$6 ==> 0.6956946870985563
| created scratch variable $6 : double
jshell> Math.random()
$7 ==> 0.3657412865477785
| created scratch variable $7 : double
jshell> Math.random()
$8 ==> 0.8828801968574324
| created scratch variable $8 : double
Note: We are not required to import java.lang package, b'z by default available.
Note: The following packages are bydefault available to the Jshell and we are not required to
import. We can check with /imports command
jshell> /imports
| 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.*
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
4 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
| import java.util.stream.*
jshell> l.add("Sunny");l.add("Bunny");l.add("Chinny");
$2 ==> true
$3 ==> true
$4 ==> true
jshell> l
l ==> [Sunny, Bunny, Chinny]
jshell> l.isEmpty()
$6 ==> false
jshell> l.get(2)
$7 ==> "Chinny"
jshell> l.get(10)
| java.lang.IndexOutOfBoundsException thrown: Index 10 out-of-bounds for length 3
jshell> l.size()
$9 ==> 3
Note: Interlly jshell having java compiler which is responsible to check syntax.If any violation we
will get Compile time error which is exactly same as normal compile time errors.
jshell> System.out.println(x+y)
| Error:
| cannot find symbol
| symbol: variable x
| System.out.println(x+y)
| ^
| Error:
| cannot find symbol
| symbol: variable y
| System.out.println(x+y)
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
5 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
jshell> Sytsem.out.println("Durga")
| Error:
| package Sytsem does not exist
| Sytsem.out.println("Durga")
| ^--------^
Note: In our program if there is any chance of getting Checked exceptions compulsory we required
to handle either by try-catch or by throws keyword. Otherwise we will get Compiletime error.
Eg:
1) import java.io.*;
2) class Test
3) {
4) public static void main(String[] args)
5) {
6) PrintWriter pw=new PrintWriter("abc.txt");
7) pw.println("Hello");
8) }
9) }
D:\durga_classes>javac Test.java
Test.java:6: error: unreported exception FileNotFoundException; must be caught or declared to be
thrown
PrintWriter pw=new PrintWriter("abc.txt");
But in the case of Jshell, jshell itself will takes care of these and we are not required to use try-
catch or throws. Thanks to Jshell.
Conclusions:
1. From the jshell we can execute any expression,any java statement.
2. Most of the packages are not required to import to the Jshell b'z by default already available to
the jshell.
3. Internally jshell use java compiler to check syntaxes
4. If we are not handling any checked exceptions we wont get any compile time errors,b'z jshell
will takes care.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
6 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UNIT - 3: Getting Help from the JShell
If You Cry For Help....JShell will provide everything.
JShell can provide complete information about available commands with full documentation,and
how to use each command and what are various options are available etc..
Agenda:
1) To know list of options allowed with jshell:
jshell>/help commandname
jshell>/command - tab
jshell> /list -
-all -history -start
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
7 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
1) To know list of options allowed with jshell
Type jshell --help from normal command prompt
D:\durga_classes>jshell --help
Usage: jshell <options> <load files>
where possible options include:
--class-path <path> Specify where to find user class files
--module-path <path> Specify where to find application modules
--add-modules <module>(,<module>)*
Specify modules to resolve, or all modules on the
module path if <module> is ALL-MODULE-PATHs
--startup <file> One run replacement for the start-up definitions
--no-startup Do not run the start-up definitions
--feedback <mode> Specify the initial feedback mode. The mode may be
predefined (silent, concise, normal, or verbose) or
previously user-defined
-q Quiet feedback. Same as: --feedback concise
-s Really quiet feedback. Same as: --feedback silent
-v Verbose feedback. Same as: --feedback verbose
-J<flag> Pass <flag> directly to the runtime system.
Use one -J for each runtime flag or flag argument
-R<flag> Pass <flag> to the remote runtime system.
Use one -R for each remote flag or flag argument
-C<flag> Pass <flag> to the compiler.
Use one -C for each compiler flag or flag argument
--version Print version information and exit
--show-version Print version information and continue
--help Print this synopsis of standard options and exit
--help-extra, -X Print help on non-standard options and exit
D:\durga_classes>jshell --version
jshell 9
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
8 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
3) To know introduction of jshell
jshell> /help intro
|
| intro
|
| The jshell tool allows you to execute Java code, getting immediate results.
| You can enter a Java definition (variable, method, class, etc), like: int x = 8
| or a Java expression, like: x + x
| or a Java statement or import.
| These little chunks of Java code are called 'snippets'.
|
| There are also jshell commands that allow you to understand and
| control what you are doing, like: /list
|
| For a list of commands: /help
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>] ...
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
9 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
| 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
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
10 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
| /list -start
| List the automatically evaluated start-up snippets
|
| /list -all
| List all snippets including failed, overwritten, dropped, and start-up
|
| /list <name>
| List snippets with the specified name (preference for active snippets)
|
| /list <id>
| List the snippet with the specified snippet id
jshell> /
/! /? /drop /edit /env /exit /help
/history /imports /list /methods /open /reload /reset
/save /set /types /vars
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
11 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
If we press tab again then we will get one line synopsis for every command:
jshell> /
/!
re-run last snippet
/-<n>
re-run n-th previous snippet
/<id>
re-run snippet by id
/?
get information about jshell
/drop
delete a source entry referenced by name or id
/edit
edit a source entry referenced by name or id
/env
view or change the evaluation context
/exit
exit jshell
/help
get information about jshell
/history
history of what you have typed
/imports
list the imported items
/list
list the source you have typed
/methods
list the declared methods and their signatures
/open
open a file as source input
/reload
reset and replay relevant history -- current or previous (-restore)
/reset
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
12 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
reset jshell
/save
Save snippet source to a file.
/set
set jshell configuration information
/types
list the declared types
/vars
list the declared variables and their values
If we press tab again then we can see full documentation of command one by one:
jshell> /
/!
Reevaluate the most recently entered snippet.
jshell> /
/-<n>
Reevaluate the n-th most recently entered snippet.
jshell> /
/<id>
Reevaluate the snippet specified by the id.
jshell> /list -
-all -history -start
jshell> /list -
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
13 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
If we press tab again then we will get synopsis:
jshell> /list -
list the source you have typed
/list
List the currently active snippets of code that you typed or read with /open
/list -start
List the automatically evaluated start-up snippets
/list -all
List all snippets including failed, overwritten, dropped, and start-up
/list <name>
List snippets with the specified name (preference for active snippets)
/list <id>
List the snippet with the specified snippet id
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
14 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Conclusions:
jshell> /list -
-all -history -start
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
15 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UNIT - 4: Understanding JShell Snippets
What are Coding Snippets?
Everything what allowed in java is a snippet. It can be
Expression,Declaration,Statement,classe,interface,method,variable,import,...
We can use all these as snippets from jshell.
jshell> System.out.println("Hello")
Hello
jshell> 10+20
$3 ==> 30
| created scratch variable $3 : int
jshell> $3>x
$4 ==> true
| created scratch variable $4 : boolean
jshell> m1()
hello
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
16 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Note: We can use /list command to list out all snippets stored in the jshell memory with snippet
id.
jshell> /list
1 : System.out.println("Hello")
2 : int x=10;
3 : 10+20
4 : $3>x
5 : String s= "Durga";
6 : public void m1()
{
System.out.println("hello");
}
7 : m1()
The numbers 1,2,3 are snippet id. In the future we can access the snippet with id directly.
Note: There are some snippets which will be executed automatically at the time jshell star-
tup,and these are called start-up snippets. We can also add our own snippets as start-up snippets.
We can list out all start-up snippets with command: /list -start
s1 : import java.io.*;
s2 : import java.math.*;
s3 : import java.net.*;
s4 : import java.nio.file.*;
s5 : import java.util.*;
s6 : import java.util.concurrent.*;
s7 : import java.util.function.*;
s8 : import java.util.prefs.*;
s9 : import java.util.regex.*;
s10 : import java.util.stream.*;
s1 : import java.io.*;
s2 : import java.math.*;
s3 : import java.net.*;
s4 : import java.nio.file.*;
s5 : import java.util.*;
s6 : import java.util.concurrent.*;
s7 : import java.util.function.*;
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
17 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
s8 : import java.util.prefs.*;
s9 : import java.util.regex.*;
s10 : import java.util.stream.*;
1 : System.out.println("Hello")
2 : int x=10;
3 : 10+20
4 : $3>x
e1 : String s =10;
5 : String s= "Durga";
6 : public void m1()
{
System.out.println("hello");
}
7 : m1()
jshell> /list 1
1 : System.out.println("Hello")
jshell> /list 1 2
1 : System.out.println("Hello")
2 : int x=10;
jshell> /list 1 5
1 : System.out.println("Hello")
5 : String s= "Durga";
We can also access snippets directly by using name.The name can be either variable name,class
name ,method name etc
jshell> /list m1
jshell> /list x
2 : int x=10;
jshell> /list s
5 : String s= "Durga";
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
18 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
We can execute snippet directly by using id with the command: /id
jshell> /3
10+20
$8 ==> 30
| created scratch variable $8 : int
jshell> /7
m1()
hello
jshell> /list
1 : System.out.println("Hello")
2 : int x=10;
3 : 10+20
4 : $3>x
5 : String s= "Durga";
6 : public void m1()
{
System.out.println("hello");
}
7 : m1()
8 : 10+20
9 : m1()
jshell> /drop $3
| dropped variable $3
jshell> /list
1 : System.out.println("Hello")
2 : int x=10;
4 : $3>x
5 : String s= "Durga";
6 : public void m1()
{
System.out.println("hello");
}
7 : m1()
8 : 10+20
9 : m1()
jshell> /4
$3>x
| Error:
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
19 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
| cannot find symbol
| symbol: variable $3
| $3>x
| ^^
Conclusions:
1. We can use /list command to list out all snippets stored in the jshell memory with snippet id.
jshell>/list
2. In the future we can access the snippet with id directly without retypinng whole snippet.
jshell>/list id
3. There are some snippets which will be executed automatically at the time jshell star-tup,and
these are called start-up snippets. We can list out all start-up snippets with command: /list -start
jshell> /list 1
7. We can access snippets directly by using name.The name can be either variable name,class
name ,method name etc
jshell> /list m1
jshell> /3
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
20 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UNIT – 5: Editing and Navigating Code Snippets
We can list all our active snippets with /list command and we can list total history of our jshell
activities with /history command.
jshell> /list
1 : int x=10;
2 : String s="Durga";
3 : System.out.println("Hello");
4 : class Test{}
jshell> /history
int x=10;
String s="Durga";
System.out.println("Hello");
class Test{}
/list
/history
1. By using down arrow and up arrow we can navigate through history.While navigating we can
use left and right arrows to move character by character with in the snippet.
2. We can Ctrl+A to move to the beginning of the line and Ctrl+E to move to the end of the line.
3. We can use Alt+B to move backward by one word and Alt+F to move forward by one word.
4. We can use Delete key to delete the character at the cursor. We can us Backspace to delete
character before the cursor.
5. We can use Ctrl+K to delete the text from the cursor to the end of line.
6. We can use Alt+D to delete the text from the cursor to the end of the word.
7. Ctrl+W to delete the text from cursor to the previous white space.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
21 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
KEY ACTION
Up arrow Moves up one line, backward through
history
Down arrow Moves down one line, forward through
history
Left arrow Moves backward one character
Right arrow Moves forward one character
Ctrl+A Moves to the beginning of the line
Ctrl+E Moves to the end of the line
Alt+B Moves backward one word
Alt+F Moves forward one word
Delete Deletes the character at the cursor
Backspace Deletes the character before the cursor
Ctrl+K Deletes the text from the cursor to the end
of the line
Alt+D Deletes the text from the cursor to the end
of the word
Ctrl+W Deletes the text from the cursor to the
previous white space.
Ctrl+Y Pastes the most recently deleted text into
the line.
Ctrl+R Searches backward through history
Ctrl+S Searches forwards through history
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
22 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UNIT – 6: Working with JShell Variables
After completing this JShell Variables session,we can answer the following:
1. Explicit variables
2. Implicit variables or Scratch variables
Explicit variables:
These variables created by programmer explicitly based on our programming requirement.
Eg:
The variables x and s provided explicitly by the programmer and hence these are explicit variables.
Implicit Variables:
Sometimes JShell itself creates variables implicitly to hold temporary values,such type of variables
are called Implicit variables.
Eg:
jshell> 10+20
$3 ==> 30
| created scratch variable $3 : int
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
23 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
jshell> 10<20
$4 ==> true
| created scratch variable $4 : boolean
The variables $3 and $4 are created by JShell and hence these are implicit variables.
jshell> $3+40
$5 ==> 70
| created scratch variable $5 : int
If we are trying to declare a variable with the same name which is already available then old
variable will be replaced with new variable.i.e in JShell, variable overriding is possible.
In JShell at a time only one variable is possible with the same name.i.e 2 variables with the same
name is not allowed.
While declaring variables compulsory the types must be matched,otherwise we will get compile
time error.
Note: By using /vars command we can list out type,name and value of all variables which are
created in JShell.
Instead of /vars we can also use /var,/va,/v
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
24 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
|
| /vars <name>
| List jshell variables with the specified name (preference for active var
iables)
|
| /vars <id>
| List the jshell variable with the specified snippet id
|
| /vars -start
| List the automatically added start-up jshell variables
|
| /vars -all
| List all jshell variables including failed, overwritten, dropped, and st
art-up
jshell> /vars
| String s = "Durga"
| int $3 = 30
| boolean $4 = true
| String x = "DURGASOFT"
| String s1 = "Hello"
jshell> /drop $3
| dropped variable $3
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
25 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
jshell> /vars
| String s = "Durga"
| boolean $4 = true
| String x = "DURGASOFT"
| String s1 = "Hello"
jshell>List<String> heroes=List.of("Ameer","Sharukh","Salman");
heroes ==> [Ameer, Sharukh, Salman]
| created variable heroes : List<String>
jshell>List<String> heroines=List.of("Katrina","Kareena","Deepika");
heroines ==> [Katrina, Kareena, Deepika]
| created variable heroines : List<String>
jshell> /vars
| String s = "Durga"
| boolean $4 = true
| String x = "DURGASOFT"
| String s1 = "Hello"
| List<String> heroes = [Ameer, Sharukh, Salman]
| List<String> heroines = [Katrina, Kareena, Deepika]
| List<List<String>> l = [[Ameer, Sharukh, Salman], [Katrina,Kareena, Deepika]]
jshell> System.out.println("Hello");
Hello
jshell> System.out.printf("Hello:%s\n","Durga")
Hello:Durga
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
26 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
$11 ==> java.io.PrintStream@10bdf5e5
| created scratch variable $11 : PrintStream
jshell> $11.printf("Hello")
Hello$12 ==> java.io.PrintStream@10bdf5e5
| created scratch variable $12 : PrintStream
jshell> /vars
| String s = "Durga"
| boolean $4 = true
| String x = "DURGASOFT"
| String s1 = "Hello"
| List<String> heroes = [Ameer, Sharukh, Salman]
| List<String> heroines = [Katrina, Kareena, Deepika]
| List<List<String>> l = [[Ameer, Sharukh, Salman],
[Katrina, Kareena, Deepika]]
| PrintStream $11 = java.io.PrintStream@10bdf5e5
| PrintStream $12 = java.io.PrintStream@10bdf5e5
FAQs:
1. What are various types of variables possible in jshell?
2. Is it possible to use scratch variable in our code?
3. Is it possible 2 variables with the same name in JShell?
4. If we are trying to declare a variable with the same name which is already available in JShell
then what will happen?
5. How to list out all active variables of jshell?
6. How to list out all active& in-active variables of jshell?
7. How to drop variables in the JShell?
8. What is the difference between print() and printf() methods?
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
27 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UNIT – 7: Working with JShell Methods
In the JShell we can create our own methods and we can invoke these methods multiple times
based on our requirement.
Eg:
jshell> m1()
Hello
jshell> m2()
New Method
In the JShell there may be a chance of multiple methods with the same name but different
argument types, and such type of methods are called overloaded methods.Hence we can declare
oveloaded methods in the JShell.
jshell> /methods
| void m1()
| void m1(int)
jshell> /methods
| void m1()
| void m2()
| void m1(int)
If we are trying to declare a method with same signature of already existing method in JShell,then
old method will be overridden with new method(eventhough return types are different).
i.e in JShell at a time only one method with same signature is possible.
jshell> /methods
| int m1(int)
Eg1: To print the number of occurrences of specified character in the given String
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
29 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
7) {
8) count++;
9) }
10) }
11) System.out.println("The number of occurrences:"+count);
12) }
jshell> charCount("Jajaja",'j')
The number of occurrences:2
jshell> sum(10,20)
The Sum:30
jshell> sum(10,20,30,40)
The Sum:100
In JShell,inside method body we can use undeclared variables and methods.But until declaring all
dependent variables and methods,we cannot invoke that method.
jshell> m1()
| attempted to call method m1() which cannot be invoked until variable x is declared
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
30 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
| created variable x : int
| update modified method m1()
jshell> m1()
10
jshell> m1()
| attempted to call method m1() which cannot be invoked until method m2() is declared
jshell> m1()
Hello DURGASOFT
jshell> m2()
Hello DURGASOFT
We can drop methods by name with /drop command.If multiple methods with the same name
then we should drop by snippet id.
jshell> /methods
| void m1()
| void m1(int)
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
31 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
| void m2()
| void m3()
jshell> /drop m3
| dropped method m3()
jshell> /methods
| void m1()
| void m1(int)
| void m2()
jshell> /drop m1
| The argument references more than one import, variable, method, or class.
| Use one of:
| /drop 1 : public void m1(){},
| /drop 2 : public void m1(int i){}
jshell> /methods
| void m1()
| void m1(int)
| void m2()
jshell> /list
jshell> /drop 2
| dropped method m1(int)
jshell> /methods
| void m1()
| void m2()
FAQs:
1. Is it possible to declare methods in the JShell?
2. Is it possible to declare multiple methods with the same name in JShell?
3. Is it possible to declare multiple methods with the same signature in JShell?
4. If we are trying to declare a method with the same name which is already there in the JShell,but
with different argument types then what will happen?
5.If we are trying to declare a method with the same signature which is already there in the
JShell,then what will happen?
6. Inside a method if we are trying to use a varaiable or method which is not yet declared then
what will happen?
7. How to drop methods in JShell?
8. If multiple methods with the same name then how to drop these methods?
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
32 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UNIT – 8: Using An External Editor with JShell
It is very difficult to type lengthy code from JShell. To overcome this problem,JShell provide in-
built editor.
jshell> /edit
diagram(image) of inbuilt-editor
If we are not satisfied with JShell in-built editor ,then we can set our own editor to the JShell.For
this we have to use /set editor command.
Eg:
jshell> /edit
But this way of setting editor is temporary and it is applicable only for current session.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
33 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
How to set default editor once again:
We have to type the following command from the jshell
FAQs:
1. How to open default editor of JShell?
2. How to configure our own editor to the JShell?
3. How to configure Notepad as editor to the JShell?
4. How to make our customized editor as permanent editor in the JShell?
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
34 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UNIT – 9: Using classes,interfaces and enum with JShell
In the JShell we can declare classes,interfaces,enums also.
We can use /types command to list out our created types like classes,interfaces and enums.
jshell> /types
| class Student
| interface Interf
| enum Colors
jshell> /edit
| created class Student
jshell> /types
| class Student
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
35 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
| created variable s : Student
jshell> s.getName()
$3 ==> "Durga"
| created scratch variable $3 : String
jshell> s.getRollno()
$4 ==> 101
| created scratch variable $4 : int
jshell> /edit
| created interface Interf
| created enum Beer
jshell> Interf.m1()
interface static method
jshell> Beer.KF.getTaste()
$8 ==> "Sour"
| created scratch variable $8 : String
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
36 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UNIT – 10: Loading and Saving Snippets in JShell
We can load and save snippets from the file.
Assume all our required snippets are available in mysnippets.jsh. This file can be with any
extension like .txt,But recommended to use .jsh.
mysnippets.jsh:
String s="Durga";
public void m1()
{
System.out.println("method defined in the file");
}
int x=10;
We can load all snippets of this file from the JShell with /open command as follows.
jshell> /list
jshell> /list
1 : String s="Durga";
2 : public void m1()
{
System.out.println("method defined in the file");
}
3 : int x=10;
Once we loaded snippets,we can use these loaded snippets based on our requirement.
jshell> m1()
method defined in the file
jshell> s
s ==> "Durga"
| value of s : String
jshell> x
x ==> 10
| value of x : int
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
37 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Saving JShell snippets to the file:
We can save JShell snippets to the file with /save command.
Note: If the specified file is not available then this save command itself will create that file.
jshell> /ex
| Goodbye
D:\>type active.jsh
int x=10;
x
System.out.println("Hello");
public void m1(){}
D:\>type start.jsh
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.*;
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
38 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
import java.util.stream.*;
Note: Bydefault,all files will be created in current working directory. If we want in some other
location then we have to use absolute path(Full Path).
Eg:
jshell> 10+20
$2 ==> 30
| created scratch variable $2 : int
jshell> System.out.println("Hello");
Hello
jshell> /list
1 : int x=10;
2 : 10+20
3 : System.out.println("Hello");
jshell> /exit
| Goodbye
D:\>jshell -v
| Welcome to JShell -- Version 9
| For an introduction type: /help intro
jshell> /list
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
39 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
1 : int x=10;
2 : 10+20
3 : System.out.println("Hello");
Eg:
jshell> /list
1 : int x=10;
2 : 10+20
3 : System.out.println("Hello");
jshell> /reset
| Resetting state.
jshell> /list
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
40 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UNIT – 11: Using Jar Files in the JShell
It is very easy to use external jar files in the jshell.We can add Jar files to the JShell in two ways.
mysnippets.jsh:
1) import java.sql.*;
2) public void getEmpInfo() throws Exception
3) {
4) Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","
scott","tiger");
5) Statement st=con.createStatement();
6) ResultSet rs=st.executeQuery("select * from employees");
7) while(rs.next())
8) {
9) System.out.println(rs.getInt(1)+".."+rs.getString(2)+".."+rs.getDouble(3)+".."+rs.getStr
ing(4));
10) }
11) con.close();
12) }
DaTabase info:
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
41 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
200..Bunny..2000.0..Hyd
300..Chinny..3000.0..Hyd
400..Vinny..4000.0..Delhi
jshell> getEmpInfo()
100..Sunny..1000.0..Mumbai
200..Bunny..2000.0..Hyd
300..Chinny..3000.0..Hyd
400..Vinny..4000.0..Delhi
Note: Internally JShell will use environment variable CLASSPATH if we are not setting CLASSPATH
explicitly.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
42 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UNIT – 12: How to customize JShell Startup
By default the following snippets will be executed at the time of JShell Startup.
s1 : import java.io.*;
s2 : import java.math.*;
s3 : import java.net.*;
s4 : import java.nio.file.*;
s5 : import java.util.*;
s6 : import java.util.concurrent.*;
s7 : import java.util.function.*;
s8 : import java.util.prefs.*;
s9 : import java.util.regex.*;
s10 : import java.util.stream.*;
mystartup.jsh:
int x =10;
String s="DURGA";
System.out.println("Hello Durga Welcome to JShell");
s1 : int x =10;
s2 : String s="DURGA";
s3 : System.out.println("Hello Durga Welcome to JShell");
Note: if we want DEFAULT import start-up snippets also then we have to open JShell as follows.
jshell> /list
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
43 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
1 : int x =10;
2 : String s="DURGA";
3 : System.out.println("Hello Durga Welcome to JShell");
s1 : import java.io.*;
s2 : import java.math.*;
s3 : import java.net.*;
s4 : import java.nio.file.*;
s5 : import java.util.*;
s6 : import java.util.concurrent.*;
s7 : import java.util.function.*;
s8 : import java.util.prefs.*;
s9 : import java.util.regex.*;
s10 : import java.util.stream.*;
Note: To import all JAVASE packages (almost around 173 packages) at the time of startup we have
to open JShell as follows.
jshell> /list
s1 : import java.applet.*;
s2 : import java.awt.*;
s3 : import java.awt.color.*;
..
s172 : import org.xml.sax.ext.*;
s173 : import org.xml.sax.helpers.*;
Note: In addition to JAVASE, to provide our own snippets we have to open JShell as follows
jshell> /list
1 : int x =10;
2 : String s="DURGA";
3 : System.out.println("Hello Durga Welcome to JShell");
s1 : import java.applet.*;
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
44 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
s2 : import java.awt.*;
s3 : import java.awt.color.*;
..
s172 : import org.xml.sax.ext.*;
s173 : import org.xml.sax.helpers.*;
Hence to print statements to the console just we can use print() or println() methods directly
instead of using System.out.print() or System.out.println() methods.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
45 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
s20 : void printf(java.util.Locale l, String format, Object... args) { System.out.printf(l, format, args);
}
s21 : void printf(String format, Object... args) { System.out.printf(format, args); }
Now onwards,to print some statements to the console directly we can use print() and println()
methodds.
jshell> print("Hello");
Hello
jshell> print(10.5)
10.5
Note:
Note:
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
46 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
UNIT – 13: Shortcuts and Auto-Completion of Commands
Shortcut for Creating Variables:
Just type the value on the JShell and then "Shift+Tab followed by v" then complete variable
declaration code will be generated we have to provide only name of the variable.
jshell> /imports
| 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.Connection
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
47 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Auto Completion commands :
1. To get all static members of the class:
jshell>classsname.<Tab>
Eg:
jshell> String.<Tab>
CASE_INSENSITIVE_ORDER class copyValueOf(
format( join( valueOf(
jshell> s.sub<Tab>
subSequence( substring(
jshell> s.substring(
substring(
jshell> s.substring(<Tab>
Signatures:
String String.substring(int beginIndex)
String String.substring(int beginIndex, int endIndex)
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
48 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
String String.substring(int beginIndex)
Returns a string that is a substring of this string.The substring begins with the character at the
specified index and extends to the end of this string.
Examples:
"unhappy".substring(2) returns "happy"
"Harbison".substring(3) returns "bison"
"emptiness".substring(9) returns "" (an empty string)
Parameters:
beginIndex - the beginning index, inclusive.
Returns:
the specified substring.
Note: Even this <Tab> short cut applicable for our own classes and methods also.
jshell> m1(<Tab>
m1(
jshell> m1(<Tab>
Signatures:
void m1(int... x)
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
49 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com