Things Developers Need To Know About JShell Features

The Java Shell Tool or known as JShell is an interactive tool used for learning Java programming language and creating Java code prototype. JShell (JShell features) was introduced in JDK 9.

JShell is an REPL(Read-Evaluate-Print-Loop) which evaluates expressions, statements and declarations. JShell REPL immediately shows results as soon as the value are entered.

The tool is run from the command line.

Writing Java programs usually involves these following process or steps :

  • Write a program.
  • Compile and Run.
  • Find an error, figure out what is wrong.
  • Edit

JShell helps you try out code in that process, while also grants you ability to explore options as you develop your program gradually.

But this doesn’t mean that JShell replace an IDE. You try out codes in the JShell, but once you know that your code works, you copy and paste the code from JShell to your IDE.

Things developers need to know about JShell features

Here are some things users/developers should know when using JShell features for rapid development

Re-declare a variable

You can’t re-declare a variable in Java. But with the help of JShell, however, you can re-declare variables based on needs without worrying about the previous declaration.

This is applicable to both primitive and reference type variables. here is an example:

jshell> Integer int=10
int ==> 5
jshell> String int="Hello"
int ==> "Hello"

Checked Exception Handling in JShell

For any JShell session, some packages are already imported by default.

So we do not have to deal with checked exceptions thrown, or even import IOException class, and the code that is compiled and execuded would work just fine, if you’re working with a single statement that is.

Howeer, if you’re writing an entire method, then you’ll need to handle checked exceptions.

To check all packages that is imported by default in any JShell session:

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.*
|    import java.io.IOException

Tab Completion In JShell

This feature is a bit similar to snippet completion, when you enter a command or command options, use Tab key to automatically complete the option or command.

If the program can determine what the completion is, then possible choices are shown.

Published by

Rafif Santika

The programming language that I specialize in is Java, because I think Java programming language is more universal and of course because I like it regardless of any reason.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.