JShell or Java Shell is a Read-Evaluate-Print Loop (REPL). This means it evaluates expressions as and when they are entered and shows us the results. JShell can be accessed from the command line and is an excellent tool to make adjustments to your code. A basic Java program needs you to go through the tedious process of writing a complete program before compiling it, fixing the errors, running it only to find that the output is not what you expected. Using a JShell helps you find problems as and when you’re developing code. You can run individual statements in a JShell and use APIs you have no idea about.
FUN FACT: The internal name for JShell is Project Kulla. The developers’ community is famous for their quirky names and this one is worth mentioning. Kulla comes from the Mesopotamian mythology as the builder god who was banished after the construction was complete. We are sure you totally can relate JShell with Kulla, as JShell is necessary only for the development period of an application.
JShell or Java Shell is a new tool introduced in NetBeans which is used to run standalone pieces of code. One can use methods without classes and context-free codes to validate them on JShell. To understand JShell and master it, one needs to understand the basic six aspects of it, beginning with the basic commands in JShell.
The second comes learning how to use variables. The third part is understanding methods and all of the related aspects like method overriding, method overload, etc. followed by the fourth part which deals with declaring, modifying and extending classes. The fifth part deals with control flow statements and enums while the sixth consists of lambda expressions. And that’s how you master JShell.
It is beyond the scope of this article to discuss each and every module of JShell in depth, however we can definitely talk about the basics, beginning with:
Once you are on NetBeans head over to the tab called Tools. Select Open Java Platform Shell from it.
That’s all! You are now on your very own Java command prompt.
Once there, go ahead and type:
This will enlist a number of commands that you can type with their usage. You can type in :
This will enlist all the packages that are imported by default as soon as you start a JShell. You can simply import all the other packages if you want to use them. Similarly, one can declare variables simply as it is done in a simple Java code snippet. System.out.println statement can be used to print the values of the variables. A thing to note here would be that a variable type has to be declared by the user and will not be declared automatically by JShell.
Implicit variables are variables automatically defined by JShell and this is a unique JShell feature. In a simple Java code, you cannot implicitly declare variables. Howevr, in JShell if you simply write ‘1’, it will create a variable $1 implicitly. Also, when you operate, JShell implicitly creates a variable to store the result of the operation. For example, if you write ‘1 + 2’, JShell will implicitly create a variable $4 ==> 3. You can also use implicit variables normally, for example $1 + $4 is allowed. A string literal like “Hello World” will also invoke implicit variables and the variable can be used in another code snippet to be printed or used.
Adding integers with decimals and such operations where widening of data types comes into picture, implicit variables play an important role.
Remember how /list lists all the code snippets? Similarly /vars lists all the variables. A tip to remember would be that JShell does not allow the top level variables to be static or final. If you try to do so, a warning will be generated. While the variable will be nevertheless created, it won’t be static or final in nature.
JShell is not an IDE
So that is all for the very basics. Once you start using JShell, you will get the gist of how to make the most of it. It’s a powerful tool for agile development and is a boon to all Java Developers. However, one should never confuse it for an IDE (Integrated Development Environment). To simulate a complete project, or to develop an application one can never rely on JShell. It is only a tool that aids a developer. Here, Netbeans is an IDE and JShell is an ‘application/command’ on its own.
JShell is a new feature in JDK9 and NetBeans 9. If you wish to use it, update your NetBeans to the latest version and get started. There isn’t much to learn afresh when dealing with JShell – just a few no-nos to keep in mind and a couple of new and exciting features to explore – and that is what makes JShell so much fun and useful.