Java 9 REPL (JShell)
REPL (Read Eval Print Loop) significa entorno de programación interactivo.
JShell es una herramienta de entorno de programación interactiva añadida en Java 9. Permite ejecutar sentencias de Java sin necesidad de usar clases o envoltorios de métodos. Es similar al intérprete de Python; puedes introducir expresiones directamente y ver su resultado de ejecución.
Ejecutar JShell
$ jshell | Welcome to JShell -- Version 9-ea | For an introduction type: /help intro jshell>
Ver comandos de JShell
Introducir/helpPuedes ver los comandos relacionados con JShell:
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
Ejecutar comandos de JShell
/importsEl comando se utiliza para ver los paquetes importados:
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>
JShell realiza cálculos
El siguiente ejemplo realiza un cálculo simple con JShell:
jshell> 3+1 $1 ==> 4 jshell> 13%7 $2 ==> 6 jshell> $2 $2 ==> 6 jshell>
Crear y usar funciones en JShell
Crea una función doubled() que multiplica por 2 el parámetro entero recibido y lo devuelve:
jshell> int doubled(int i){ return i*2;}
| created method doubled(int)
jshell> doubled(6)
$3 ==> 12
jshell>
Salir de JShell
Introduce el comando /exit para salir de jshell:
jshell> /exit | GoodbyeOtras extensiones
Java 9 Nuevas características