GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java - REPL (JShell)
Selected Reading
Java - REPL (JShell)Introduction to REPL (JShell)REPL stands for Read Evaluate Print Loop. JShell was introduced in Java 9 which is an interactive console. JShell as REPL allows to run arbitrary snippet of java code in console without need to save and compile java code file. This facility is very important to test codes quickly like evaluating regular expression, checking formating of strings, date formats etc. JShell reads each line entered, evaluates it and then print the result and then again becomes ready for next set of input. Advantages of Using JShellThis capability of JShell gives developers following advantages -
Running JShellOpen command prompt and type jshell. D:\test>jshell | Welcome to JShell -- Version 20.0.2 | For an introduction type: /help intro With JShell, we can test methods, classes, expressions as well. In following examples, let's explore some of the features of JShell. Create and Invoke Method in JShellFollowing snippet showing a sample "Hello World" program in JShell. Here, we've created a method greet() which has a single statement to print a message as "Hello World!". As next, we've invoked the method greet() and result is printed on the console. Hello World Example in JShelljshell> void greet() { System.out.println("Hello World!");}
| created method greet()
jshell> greet()
Hello World!
jshell>
Creating Variables in JShellFollowing snippet shows how to create variables in JShell. semi-colon is optional. We can create objects as well in JShell. If a variable is not initialized then it is given a default value or null if it is an object reference. Once a variable is created, it can be used as shown in the last statement where we've used the string variable to print its value. Examplejshell> int i = 10 i ==> 10 jshell> String name = "Mahesh"; name ==> "Mahesh" jshell> Date date = new Date() date ==> Fri Feb 02 14:52:49 IST 2024 jshell> long l l ==> 0 jshell> List list list ==> null jshell> name name ==> "Mahesh" Evaluate Expression in JShellFollowing snippet shows how to evaluate an expression using JShell. Here we've passed a statement which returns a formatted string. JShell automatically created a String variable $9 and assigned it the result. As next statement, we've printed. Examplejshell> String.format("%d pages read.", 10);
$9 ==> "10 pages read."
jshell> $9
$9 ==> "10 pages read."
Jshell Built-In CommandsJShell provides various commands to list the variables created, methods created, imports used etc. Some of the important JShell commands are -
Example: Demonstrating /help CommandWe can view all commands using /help option. 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 | /drop <name or id> | delete a source entry | /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 type declarations ... Example: Demonstrating /vars CommandIn following example, we've used /vars command to print the variables declared during a session. C:\Users\Mahesh>jshell | Welcome to JShell -- Version 20.0.2 | For an introduction type: /help intro jshell> int i = 10 i ==> 10 jshell> String name="Mahesh" name ==> "Mahesh" jshell> /vars | int i = 10 | String name = "Mahesh" jshell> Example: Demonstrating /imports CommandWe can use /imports command to check the imports available in JShell as shown below: 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.* jshell> Exiting JShellWe can use /exit command to exit JShell as shown below: Examplejshell> /exit | Goodbye C:\Users\Mahesh> Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |