Category: Java

Learning Java Using JShell

Java programming is not hard to learn, but proficiency in the language takes time, effort, and a lot of practice. JShell can significantly reduce the time required to learn many of the core features of the Java programming language.

As a prototyping tool, JShell makes it easy to write and develop a code snippet without having to compile and run a complete program to test it.

What is JShell?

Java 9 witnessed the introduction of JShell, and it persists with the latest releases. It is a read-evaluate-print-loop (REPL) tool that can be used to write and execute Java code, load code from a file, save code snippets, and more, all using a simple command-line interface.

Learning Java using JShell

Learning Java using JShell, by Christoph Tornau (Packt Publishing), is an excellent introduction to the Java programming language using JShell and is especially suited for beginners. While it does not cover all the formalities of writing a full Java application, it does cover the fundamentals of the language itself including:

  • Data types: int, byte, double, float, String, char, and boolean.
  • Variables: Declaring variables.
  • Operators: Using logical, conditional, compound assignment and unary operators.
  • If and If-Else statements
  • Switch expressions: Note that switch expressions are a preview feature and are disabled by default. Run JShell with “–enable-preview” to enable switch expressions.
  • Loops: While, Do-While, For, and ForEach
  • Arrays
  • Methods
  • Object-Oriented Programming
  • Classes and Objects

Using JShell to teach the fundamentals of Java is a smart approach taken by Christoph. It eliminates the distractions of having to choose from a variety of editors or IDE’s (Interactive Development Environment) and provides a simple “clutter-free” means to focus on Java alone.

The video course is ideally suited for beginning Java programmers and serves as a soft introduction to using JShell with Java.

Interestingly, Learning Java using JShell does not spend much time covering many of the JShell commands and key combinations that make using it that much more effective. What follows is a brief introduction to JShell. I have also included some helpful links to additional articles and resources at the end of this post.

Java Version 9 or Later

To run JShell, you must have JDK9 or later installed on your computer. You can get the latest version of the JDK for your operating system from Oracle’s “Java SE Downloads” page.

You can check the installed version of Java by typing “java -version” at the command prompt in a terminal session as pictured below:

Running JShell

To run JShell on windows, start a terminal session by typing “cmd” in the search bar and click on the “Command Prompt – app.”

When the command prompt appears, type “jshell” and press enter. Some Java language features, such as switch expressions, are available in preview mode only and disabled by default. They can be enabled by typing “jshell –enable-preview” at the command prompt as pictured below:

For a brief introduction to JShell, type “/help intro” without the quotes as suggested when the jshell opens in the terminal. For a list of commands, type /help. Oracle’s Java Platform, Standard Edition Java Shell User’s Guide, provides an in-depth review of JShell’s core features.

You can set the feedback mode using the “/set feedback mode” command where the mode is replaced by either “verbose,” “normal,” “concise,” or “silent.” For example: “/set feedback silent” displays the absolute minimum amount of information. You can also use the /set command to create customized feedback and prompt settings.

For help on a specific command or subject, type /help followed by the command or subject of interest to you. For example, when you enter “/help /list” at the jshell prompt, the requested help information appears on the screen as pictured below:

JShell Commands

The JShell commands available in version 12 as displayed on our system are as follows:

  • /list [|-all|-start]
    • list the source you have typed
  • /edit
    • edit a source entry
  • /drop
    • delete a source entry
  • /save [-all|-history|-start]
    • save snippet source to a file
  • /open
    • open a file as the input source
  • /vars [|-all|-start]
    • list the declared variables and their values
  • /methods [|-all|-start]
    • list the declared methods and their signatures
  • /types [|-all|-start]
    • list the type declarations
  • /imports
    • list the imported items
  • /exit []
    • exit the jshell tool
  • /env [-class-path ] [-module-path ] [-add-modules ] …
    • view or change the evaluation context
  • /reset [-class-path ] [-module-path ] [-add-modules ]…
    • reset the Jshell tool
  • /reload [-restore] [-quiet] [-class-path ] [-module-path ]…
    • reset and replay relevant history — current or previous (-restore)
  • /history [-all]
    • history of what you have typed
  • /help [|]
    • get information about using the jshell tool
  • /set editor|start|feedback|mode|prompt|truncation|format …
    • set configuration information
  • /? [|]
    • get information about using the jshell tool
    • same as /help
  • /!
    • rerun last snippet — see /help rerun
  • /
    • rerun snippets by ID or ID range — see /help rerun
  • /-
    • rerun n-th previous snippet — see /help rerun

JShell Subjects

To learn more about a given subject, enter one of the subjects from the list below after the /help command. For example: /help intro

  • intro: an introduction to the JShell tool.
  • keys: a description of line editing support to navigate and edit snippets and commands.
  • id: a description of snippet IDs and how to use them
  • shortcuts: a description of keystrokes for snippet and command completion, information access, and automatic code generation
  • context: description of the evaluation context options for /env /reload and /reset
  • rerun: a description of ways to re-evaluate previously entered snippets
  • shortcuts

The “jshell>” prompt doesn’t offer help unless you ask for it. There is more to this seemingly “archaic” interface than first meets the eye. and it is worth taking the time to look further.

Why use JShell?

As a developer, it is convenient to test a snippet of code without having to formally compile and run it to see if it’s going to work. Working with APIs can also be a challenge. and JShell is a perfect environment to learn more about them.

JShell is even integrated into the JetBrains IntelliJ IDE so you can reap all the benefits and productivity gains it has to offer and makes knowing what JShell is and what it can do for you even more relevant.

Some seasoned Java programmers are not aware that JShell even exists. I can only stress the importance of staying current with updates when they occur as they usually introduce new features and ways to make writing code that much more efficient.

As I’ve said many times before, “There’s always a better way and more than one solution.” Learning Java using JShell by Christoph Tornau is yet another example of that.

Until Next Time – STAY lean!

Related Articles and Resources

Advertisements

Machine Learning Algorithms

The next book adventure is “Machine Learning Algorithms” by Giuseppe Bonaccorso, Packt Publishing, (Jul 2017), 360 pages.

My initial preview suggests there is a lot to learn and so little time. Perhaps its time to let machines do more of the work.

Until Next Time – Stay lean!

Related Articles and Resources

  • Python Machine Learning – Second Edition by Sebastian Raschka and Vahid Mirjalili, Pack Publishing, (Sep 2017).
  • Machine Learning for Developers by Rodolfo Bonnin, Pack Publishing, (Oct 2017).
  • Statistic for Machine Learning by Pratap Dangeti, Packt Publishing, (Jul 2017).
  • Mastering Java Machine Learning by Dr. Udav Kamath and Krishna Choppella, Pack Publishing, (Jul 2017), 556 pages.
  • Java Machine Learning for Computer Vision (Video) by Klevis Ramo, Pack Publishing, (Jul 2018), 5 hours 6 minutes.
  • Machine Learning in Java – Second Edition – by AshishSingh Bhatia and Bostjan Kaluza, Packt Publishing, Nov 2018, 300 pages.

JavaFX-12.0.1

With the recent release of Java Development Kit 12 (JDK 12), it’s time to upgrade to JavaFX-12.0.1 as well. JavaFX is now managed and released as it’s own independent entity and is not shipped as part of the Java Development Kit.

If you are new to Java and JavaFX, visit Getting Started with JavaFX 12 to install and test the latest version of JavaFX. Instructions are presented for use with Linux/Mac and Windows.

If you already have Java 12 installed, download the JavaFX runtime, unzip the file to your location of choice, and add the PATH_TO_FX environment variable: set PATH_TO_FX=”path\to\javafx-sdk-12\lib”

Now you’re ready to test the installation using the HelloFX.java sample which is available for download from GitHub.

From the command line, use javac to compile the program as follows:

  • javac –module-path %PATH_TO_FX% –add-modules javafx.controls HelloFX.java

Now execute the program using:

  • java –module-path %PATH_TO_FX% –add-modules javafx.controls HelloFX

If all went as planned, running the program should produce a window as pictured below:

Why JavaFX?

Every book, online tutorial, or course typically begin with writing programs that run on the console or terminal. An initial frustration for new users is that Interactive Development Environments or IDE’s such as Eclipse, NetBeans, or Visual Studio Code all use a Graphical User Interface or GUI interface and leaves us wondering why we can’t do graphics.

Of course, the books and tutorials do eventually address GUI’s; however, the subject of GUI’s is a topic for later chapters or course segments. Since JavaFX is not part of the JDK distribution, you must download it separately.

Until Next Time – STAY lean!

Related Articles and Resources