Here at IDRsolutions we are very excited about Java 9 and have written a series of articles explaining some of the main features. In our previous Java 9 series article we looked at JLink in Java 9. This time we will be looking at JShell.
How many times have you had to knock up a new class, or hunt for a main method so that you can write and execute a little snippet of code? Or you just wanted to test some code (does >> divide or multiply by two again?). In Java9 this is now a simple operation because Java9 includes a new feature called JShell.
What is JShell?
JShell introduces a REPL (Read-Eval-Print-Loop) environment in Java when Java 9 is released. With this new style of writing Java code, you can write and test small snippets of code quickly and easily. JShell will compile and run the code there and then.
How does JShell work?
JShell gives you a prompt (and interactive session history) so that you can type and execute Java commands. It is really that simple! We can see the lines of code we have put into JShell as it keeps a log of it on screen.
The adaptability of JShell will enable us to use it with any tool whether that is CLI or an IDE. JShell will have an API to enable it to work with other applications and tools. We can use it through the CLI or we can use it through an application and add our own cool features with it. For example, NetBeans has a feature where it can create a class and move all the code from the current JShell instance into it.
Let’s give it a try.
I am running JShell using the NetBeans IDE (a development build). The latest NetBeans nightly builds already let you experiment with Java9.
As you can see above, I have set an age and name as variables within the shell. I then printed out both variables together and it only took me 3 lines of code. One line to set the age, one line to set the name and one line to print them both out.
JShell keeps everything within memory until the session ends. It would remember the age and name variables until the session has ended. We would have to set the variables again next time we start JShell.
Why is JShell useful?
JShell makes it really easy to write, execute and test fragments of code. So you can perform quick tasks, test and mockup code.
JShell would be a great tool for teachers and students. With more interactivity that JShell will bring, students would be able to engage more with the technology and understand how it works better. JShell will enable teachers to provide their students with a step-by-step usage of Java so they can start small and learn in incremental steps.
JShell is a much welcomed feature which will only make Java easier to use and learn. What will you be using it for?
Latest posts by Zain (see all)
- Java 9 JShell explained in 5 minutes - May 11, 2017
- Java 9 Modularity explained in 5 minutes - May 4, 2017
- Key takeaways from NetBeans Day UK 2017 - April 26, 2017
- Why IDRsolutions uses the NetBeans IDE for Java Development - April 18, 2017
- 5 Reasons to Attend NetBeans Day UK 2017 - April 6, 2017