The REPL: JShell
JShell is a Read–Eval–Print Loop shipped with the JDK. Use it whenever you want to try something out without creating a file.
Semicolons are optional. Results without a variable get names like $1, which you can reuse.
Declarations work too
Redefining a method simply replaces it.
Commands
| Command | Does |
|---|---|
/help |
list all commands |
/vars /methods /types |
what you have defined |
/list |
code so far, numbered |
/edit 3 |
edit snippet 3 |
/save f.jsh /open f.jsh |
save / load a session |
/imports |
the automatic imports |
/reset |
start over |
/exit |
quit |
java.util, java.io, java.math, java.net, java.util.stream and
java.util.function are imported for you:
Shortcuts
- Tab completes names:
"text".to+ Tab lists matching methods. - Shift+Tab, then
vturns the expression you just typed into a variable declaration.
★ Exercises
- Compute how many seconds are in a year.
- Declare
nameand print a greeting with it. - Define
isEven(int)and test it with five values. - Use Tab completion to list
Stringmethods starting withstr. /saveyour session,/reset, then/openit again.