Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java 9 REPL (JShell): Interactive Coding & Testing Made Simple

REPL stands for Read-Eval-Print Loop. With JShell, java has REPL capability. Using REPL, we can code and test java based logic without compiling using javac and see the result of calculations directly.

Running JShell

Open command prompt and type jshell.

$ jshell
|  Welcome to JShell -- Version 9-ea
|  For an introduction type: /help intro
jshell>

Viewing JShell commands

Type /help once jshell command starts running.

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

Running JShell command

Type /imports once jshell command starts running and see the used 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.*
jshell>

Running Calculations in JShell.

Try running simple calculations in JShell.

jshell> 3+1
$1 ==> 4
jshell> 13%7
$2 ==> 6
jshell> $2
$2 ==> 6
jshell>

Creating and using functions in JShell

Create a function doubled() to take int and return its doubled value.

jshell> int doubled(int i){ return i*2;}
|  created method doubled(int)
jshell> doubled(6)
$3 ==> 12
jshell>

Exiting JShell

Type /exit.

jshell> /exit
| Goodbye

Java

  1. Master Java Operators: Types, Syntax, & Practical Examples
  2. Java Comments: Types, Usage, and Best Practices
  3. Mastering Java if…else: Control Flow Explained
  4. Mastering the Java Enhanced For Loop: Syntax, Examples, and Best Practices
  5. Java Break Statement: How, When, and Labeled Breaks Explained
  6. Mastering Java's super Keyword: Advanced Usage & Practical Examples
  7. Mastering Java Interfaces: Concepts, Implementation, and Best Practices
  8. Mastering Java Try‑with‑Resources: Automatic Resource Management Explained
  9. Java Annotations Explained: Types, Placement, and Practical Examples
  10. Java 9 REPL (JShell): Interactive Coding & Testing Made Simple