Java9 Repl
# Java 9 REPL (JShell) Tutorial
REPL stands for **Read-Eval-Print Loop**. It is an interactive programming environment that takes single user inputs, evaluates them, and immediately returns the result to the user.
Introduced in Java 9, **JShell** is the official interactive command-line tool that provides a REPL environment for the Java programming language. It allows you to execute Java statements, evaluate expressions, and declare variables or methods directly without wrapping them inside a class or a `main` method. This is highly similar to interactive interpreters in languages like Python, Ruby, or Node.js, making prototyping and learning Java significantly faster and easier.
---
## Starting JShell
To launch JShell, open your terminal (macOS/Linux) or Command Prompt/PowerShell (Windows) and type the following command:
```bash
$ jshell
```
Upon startup, you will see a welcome message indicating the version of JShell you are running, followed by the `jshell>` prompt:
```text
| Welcome to JShell -- Version 9-ea
| For an introduction type: /help intro
jshell>
```
---
## Exploring JShell Commands
JShell provides a set of built-in commands to manage your environment, inspect variables, and control your session. All JShell commands are prefixed with a forward slash (`/`).
To view the complete list of available commands, type `/help` at the prompt:
```text
jshell> /help
| Type a Java language expression, statement, or declaration.
| Or type one of the following commands:
| /list [|-all|-start]
| list the source you have typed
| /edit
| edit a source entry referenced by name or id
| /drop
| delete a source entry referenced by name or id
| /save [-all|-history|-start]
| Save snippet source to a file.
| /open
| open a file as source input
| /vars [|-all|-start]
| list the declared variables and their values
| /methods [|-all|-start]
| list the declared methods and their signatures
| /types [|-all|-start]
| list the declared types
| /imports
| list the imported items
```
---
## Common JShell Operations
### 1. Viewing Default Imports
By default, JShell pre-imports several commonly used Java packages so you don't have to import them manually. You can view these imports using the `/imports` command:
```text
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>
```
### 2. Performing Basic Calculations
You can evaluate expressions directly. If you do not assign the result of an expression to a variable, JShell automatically creates an implicit scratch variable (e.g., `$1`, `$2`) to store the value.
```text
jshell> 3+1
$1 ==> 4
jshell> 13%7
$2 ==> 6
jshell> $2
$2 ==> 6
```
*Note: You can reference these scratch variables in subsequent expressions.*
### 3. Declaring and Using Methods
You can define custom methods directly in the shell without wrapping them in a class structure.
Let's create a method named `doubled` that takes an integer parameter and returns its value multiplied by 2:
```text
jshell> int doubled(int i){ return i*2;}
| created method doubled(int)
jshell> doubled(6)
$3 ==> 12
```
### 4. Listing Declared Variables and Methods
If you want to see all the variables you have declared during your session, use the `/vars` command:
```text
jshell> int x = 42;
x ==> 42
jshell> /vars
| int $1 = 4
| int $2 = 6
| int $3 = 12
| int x = 42
```
To see all active methods, use the `/methods` command:
```text
jshell> /methods
| int doubled(int)
```
---
## Exiting JShell
To terminate your session and exit the JShell environment, use the `/exit` command:
```text
jshell> /exit
| Goodbye
```
---
## Key Considerations & Best Practices
* **No Semicolons Required:** For simple, single-line statements and expressions, semicolons are optional in JShell. However, they are still required if you put multiple statements on a single line.
* **Forward References:** JShell allows you to define methods that reference other methods or variables that have not been defined yet. They will become fully functional once the missing components are declared.
* **Session Persistence:** Code written in JShell is temporary. If you want to save your work, use the `/save ` command to export your session snippets to a file, which can later be reloaded using `/open `.
YouTip