Skip to content

Instantly share code, notes, and snippets.

@mjg123

mjg123/blog.md Secret

Created November 6, 2017 11:32
Show Gist options
  • Save mjg123/a1acb27b6fc39c8e4be0309fbc1e766a to your computer and use it in GitHub Desktop.
Save mjg123/a1acb27b6fc39c8e4be0309fbc1e766a to your computer and use it in GitHub Desktop.

jshell is a new-in-Java-9 tool - a REPL for Java!! I was not sure at first how useful it would be but since I started playing with it I've been finding more and more uses for it, and it's pretty cool. There's a couple of surprising things though.

A quick look

https://gist.github.com/405d785bdd5f7b65b05b517d141122e0

and we're at the REPL prompt. We can do a few totally unsurprising (but neat!) things:

https://gist.github.com/67107d09fb1492df9fb09bd2c21db720

There's also a few meta commands, try starting with /help

The $n are references to the results:

https://gist.github.com/778a155324b868a5e46aaec37ad105db

etc. We can create, modify and replace classes on the fly:

https://gist.github.com/3d2657735f2ef4025ae3001fdc668595

Classes are created then modified or replaced when changed. If the change alters a method signature or adds/removes a field then the class is replaced, otherwise it is just modified. What happens to instances when you do that?

Modifying a class

Modifying a class alters existing instances:

https://gist.github.com/a0ccc45e3aadb46df7694f726ba2ec6e

Replacing a class

Replacing a class nullifies instance references:

https://gist.github.com/af2492dca98de97a4b758a6033f2ba04

Semicolon insertion

My inner Douglas-Crockford is bristling at this a bit, to be honest. Semi-colons are optional. You might notice I haven't used any in the code above. The rule seems to be that if an expression can be evaluated, then it will be. So be careful typing code like:

https://gist.github.com/f2951aeabbe0d03ef12f12029d2af66f

Because you'll get this:

https://gist.github.com/fcf3e30279260364d528bb2d38e71d39

I use three ways to cope with this:

  • Everything on one line. Not nice with long lines.

https://gist.github.com/6549f0aab36ba57be1ddf52fdc3b45c3

  • Use the backreferences. Usually only used when I forget about semicolon insertion, and accompanied by me kicking myself.

https://gist.github.com/234c9086ad6e7ad847bfedefc390a0af

  • Move your dots. A bit surprising-looking but works OK. Least-worst option IMHO.

https://gist.github.com/ca4fd92fa7e3419c55db57224b6eaff0

NB there is an open bug to prevent this behaviour during multi-line paste

Pasting content

There seems to be a bug which prevents pasting more than 2 lines of code. It's reported here. That bug is marked as RESOLVED but still the bug persists in JDK 9.0.1 which is the latest one (on Linux at least). The workaround is:

https://gist.github.com/87c4fde93793912bf88565707cb6fb06

This launches your $EDITOR which you can paste as many lines as you like into, then save & exit and jshell evaluates it all.

Conclusion

jshell is a nice way to play with Java code. Much nicer than creating dummy classes with a main() method. Trisha Gee shows us a nice demo of how to use jshell from IntelliJ - looks great, and I assume other IDEs have similar support. Try it out :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment