Welcome to JShell

Welcome to JShell

Monday, November 27, 2017

Another year of Devoxx and that means another interesting talk of Venkat Subramaniam! This time the topic was about the new features and additions of Java 9. One of the coolest additions to Java in jdk 9 is JShell. JShell is a REPL for java. Almost every language has a REPL. Now Java has one too!

What’s REPL?

REPL stands for Read-Evaluate-Print-Loop. With JShell, you can write lines of code and it will be evaluated immediately. So, it reads your input, evaluates and prints it and then loops this process. To start JShell, install java 9 and just type “jshell” in your cmd.  

C:\>jshell
|  Welcome to JShell -- Version 9.0.1
|  For an introduction type: /help intro

To exit, type ‘/exit’. It will even be so friendly to say goodbye.

jshell> /exit
|  Goodbye

 

Why should you use it?

You can use JShell for prototyping, experimenting or even for quickly debugging pieces of incremental code. It’s also a great learning tool for java beginners. Remember when you first learned how to print “Hello World” in a java class? You first had to go through all these strange words like public, void, static or ‘String[] args’. If you’re a teacher, you can use JShell and just type:

jshell> System.out.println("Hello World")
Hello World

JShell will print “Hello World”. And for the first time ever, you don’t have to use a semicolon in java! Unless, you want to write entire methods, then the semicolon is still required and the syntax has to be complete. But the point is, you can just write snippets of code and java will evaluate it on the spot. For example, this expression will save the result in a variable $2. Java will evaluate the expression “4+3” and save the result. After that, you can reuse the variable again in a different expression “$2 + 15 = 22”. It outputs 22 because 7 + 15 = 22.

jshell> 4 + 3
$2 ==> 7

jshell> $2 + 15
$3 ==> 22

 

Auto-complete and list of available methods

One of the best things about IDE’s is the auto-complete function. JShell also has this to a certain extend. For example, create a String object, type ‘.’ (dot) after the variable and press TAB to get the list all available methods.

jshell> String greetings = "hello"
greetings ==> "hello"

jshell> greetings.
charAt(                chars()                codePointAt(
codePointBefore(       codePointCount(        codePoints()
compareTo(             compareToIgnoreCase(   concat(
contains(              contentEquals(         endsWith(
equals(                equalsIgnoreCase(      getBytes(
getChars(              getClass()             hashCode()
indexOf(               intern()               isEmpty()
lastIndexOf(           length()               matches(
notify()               notifyAll()            offsetByCodePoints(
regionMatches(         replace(               replaceAll(
replaceFirst(          split(                 startsWith(
subSequence(           substring(             toCharArray()
toLowerCase(           toString()             toUpperCase(
trim()                 wait(

Or you can make JShell auto-complete but typing the beginning of a method and then hit the TAB button.

jshell> greetings.con
concat(          contains(        contentEquals(

jshell> greetings.concat(
concat(

Press TAB again to get the Javadoc!

Lists of commands and variables

You can get the list of all the lines of code you wrote by typing ‘/list’. Then, you can type ‘/n’ to execute that specific command again. Press ‘/save’ to save your input to a file.

jshell> /list

   1 : System.out.println("Hello World")
   2 : 4 + 3
   3 : $2 + 15
   4 : String greetings = "hello";

jshell> /1
System.out.println("Hello World")
Hello World

‘/v’ gives you a list of all variables you have made so far. ‘/m’ for all the methods you’ve created. ‘/t’ for all the types (classes) you’ve made.

jshell> /v
|    int $2 = 7
|    int $3 = 22
|    String greetings = "hello"

‘/imports’ gives you a list of all the current imported packages. Check out all these default imports. Some are useful, but others are not. Why does java think we use regex all the time?

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.*

 

Edit & drop

So far, we’ve added new variables. To change the value, you can do it ‘the old way’ by just re-initializing  the value. But with JShell, you can now type ‘/edit varname’ and this popup window appears on your screen to edit your variable! ‘/edit’ shows you the same screen but, it lists all the variables and you can just edit them in the screen. Could be useful for more complex code. ‘/drop x’ will remove the variable named x.

Final

You can not use final in JShell. “It doesn’t want you to start using legal terms” – Venkat.

Conclusion

JShell offers you a completely different way of programming in java. It’s very lightweight. It’s basically an experimenting tool in java for you to quickly try out some code. At least that is what I’ll be using JShell for most of the time. I can’t really see myself using this for more complex stuff.

 

/LD

 

Newsletter

Stay up to date with our newsletter.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.