Java 9 REPL (JShell): Codificación y Pruebas Interactivas Simplificadas
REPL significa Read-Eval-Print Loop (Bucle Leer-Evaluar-Imprimir). Con JShell, Java tiene capacidad REPL. Usando REPL, podemos codificar y probar lógica basada en Java sin compilar con javac y ver el resultado de los cálculos directamente.
Ejecutar JShell
Abre la línea de comandos y escribe jshell.
$ jshell
| Welcome to JShell -- Version 9-ea
| For an introduction type: /help intro
jshell>
Ver Comandos de JShell
Escribe /help una vez que el comando jshell comience a ejecutarse.
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 Comando JShell
Escribe /imports una vez que el comando jshell comience a ejecutarse y ve las importaciones usadas.
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>
Ejecutar Cálculos en JShell
Intenta ejecutar cálculos simples en 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 duplicado() que tome un int y devuelva su valor duplicado.
jshell> int duplicado(int i){ return i*2;}
| created method duplicado(int)
jshell> duplicado(6)
$3 ==> 12
jshell>
Salir de JShell
Escribe /exit.
jshell> /exit
| Goodbye
Java
- Dominar Operadores en Java: Tipos, Sintaxis y Ejemplos Prácticos
- Comentarios en Java: Tipos, Uso y Mejores Prácticas
- Dominar el Bucle for Mejorado en Java: Sintaxis, Ejemplos y Mejores Prácticas
- Dominar Strings en Java: Creación, Métodos y Mejores Prácticas
- Dominando interfaces en Java: Conceptos, implementación y mejores prácticas
- Dominando las clases internas anónimas en Java: Definición, sintaxis y ejemplos prácticos
- Dominando Try‑with‑Resources en Java: Gestión automática de recursos explicada
- Anotaciones en Java explicadas: Tipos, ubicación y ejemplos prácticos
- Aserciones en Java: Guía práctica para detectar bugs y asegurar fiabilidad del código
- Entendiendo Java Vector: Uso, Características y Comparación con ArrayList
- Java 9 REPL (JShell): Codificación y Pruebas Interactivas Simplificadas