In this tutorial, we will see the usage of commonly used JShell commands with an example.
The Java Shell tool (JShell) is an interactive tool for learning the Java programming language and prototyping Java code. JShell is a Read-Evaluate-Print Loop (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results.
Complete the Java JShell tutorial at https://www.javaguides.net/2022/06/java-shell-jshell-tutorial.html
Java JShell Commands
JShell Start
JShell is included in JDK 9. To launch the JShell tool, we just need to type the jshell command at the command line:rameshfadatare@Rameshs-MacBook-Air rameshfadatare % jshell
| Welcome to JShell -- Version 17.0.2
| For an introduction type: /help intro
JShell Help Intro
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 the jshell tool commands that allow you to understand and
| control what you are doing, like: /list
|
| For a list of commands: /help
Leave/Exit JShell
Use /exit command to exit from JShell:jshell> /exit
| Goodbye
Reset or Restart Session
To discard all entered snippets and restart the session, we can use the /reset command:jshell> /reset
| Resetting state.
List Snippets
To list all the snippets, we can use the /list command as follows:jshell> /list
1 : import java.time.*;
2 : System.out.println(LocalDate.now())
3 : public interface Vehicle {
String getBrand();
String speedUp();
String slowDown();
default String turnAlarmOn() {
return "Turning the vehice alarm on.";
}
default String turnAlarmOff() {
return "Turning the vehicle alarm off.";
}
static int getHorsePower(int rpm, int torque) {
return (rpm * torque) / 5252;
}
}
jshell>
List Variables
To display the name, type, and value of variables that were entered, we can use the /vars command as following forms:jshell> /vars
| int a = 10
| String name = "java guides"
| float f = 10.0
List Methods
jshell> void printNumbers(int n) {
...> for(int i=0; i< n; i++) {
...> System.out.println(i);
...> }
...> }
...>
| created method printNumbers(int)
jshell> void printSquaresOfNumbers(int n) {
...> for(int i=0; i< n; i++) {
...> System.out.println(i*i);
...> }
...> }
...>
| created method printSquaresOfNumbers(int)
jshell> /methods
| void printNumbers(int)
| void printSquaresOfNumbers(int)
Display Classes, Interfaces, and Enums
To displays classes, interfaces, and enums that were entered, we can use the /types command:jshell> /types
| interface Vehicle
| enum Month
View Java Imports
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.*
Edit Snippets
jshell> /edit
| replaced enum Month
Drop Snippets
To delete a snippet which can be variables, types, imports, etc, we can use the /drop command:jshell> int abc = 50;
abc ==> 50
jshell> drop abc
| replaced variable abc, however, it cannot be referenced until class drop is declared
Save Snippets
/save [options] fileExample:
Open Snippets
To open the script specified and reads the snippets into the tool, we can use the /open command:jshell> /open dem-jshell.jsh
Comments
Post a Comment
Leave Comment