What Is JShell?
● JShell is an interactive tool (REPL: Read-Evaluate-Print Loop) introduced in
Java 9 that lets you run Java code snippets line by line, immediately seeing
the results.
● Ideal for learning Java, prototyping code, and experimenting with APIs before
writing full programs.
How to Launch JShell
1. Prerequisite: Ensure Java 9 or later is installed.
Check with: java -version
2. Open Command Prompt/Terminal.
3. Start JShell:
Type jshell and press Enter.
● You’ll see a welcome message and a prompt like jshell>.
Running Commands in JShell
● At the jshell> prompt, type Java statements, declarations, or expressions
(no need for a full class or main method).
● Example:
● java
jshell> int x = 5;
jshell> [Link](x * 2);
jshell> String greet = "Hello, World!";
jshell> greet
●
● Use special JShell commands for session management and viewing info (all
start with /):
● /vars — shows variables
● /methods — lists methods
● /list — lists all entered snippets
● /help — displays help.
How to Exit JShell
● Type /exit and press Enter.
● You will see | Goodbye and return to the command line.
● Alternatively, press Ctrl+D (signals end-of-file; works on Linux/Mac).
Benefits of JShell for Beginners
● Immediate Feedback: Quickly test ideas and see output without writing entire
programs
● Simplifies Experimentation: No need to set up project files or write boilerplate
code.
● Learning Aid: Try Java syntax, variables, methods, and unfamiliar APIs in a
safe environment.
● Error Exploration: Easily spot mistakes and correct them on the fly.
● Fast Prototyping: Prototype new features, algorithms, or troubleshoot
concepts before adding to a real project
JShell is especially useful for students and newcomers, making it much easier to
learn the basics of Java in an interactive, forgiving environment before advancing to
full-scale projects!