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

Ruby Programming For Everyone

Ruby is a great language, especially for beginners. The syntax of Ruby is intuitive, easy to understand, and easy to get started. If you have no prior programming experience and 1 hour and 44 minutes of time you can learn Ruby.

Ruby Programming For Everyone by John Elder (Packt Publishing – ISBN 9781839211997) is a video training program designed to take you from the absolute basics to advanced Ruby programming techniques in less than 2 hours.

You will learn how to install Ruby on your computer and rapidly progress from the traditional “Hello World” program to more advanced Ruby programming techniques. Each video is short and concise, allowing you to learn at a pace that’s ideal for you.

Ruby is one of my favourite languages simply because it’s easy to get things done quickly. If you’ve never programmed before, or simply want to see what Ruby has to offer, Ruby Programming for Everyone is a great way to get started. You’ll be surprised how easy it can be.

Until Next Time – STAY lean!

Related Articles and Resources

Lean Programming – 5S for Code

I enjoy coding and learned many programming languages over the years. As diverse as these languages are, there is one trait that is consistent among them all: every instruction matters.

The principles of Lean, defined as “the pursuit of perfection to achieve excellence through the continual elimination of waste,” can also be applied to programming. The seven forms of waste are:

  • Defects: Bugs in our Code. Avoid using clever tricks or complicated code where a simple statement or expression is just as effective.
  • Overproduction: Features or functions that are not required,
  • Waiting: Synchronous versus asynchronous processes, load times, code sequencing, multi-core threading, distributed networking,
  • Inventory: Redundant Code,
  • Motion: Inefficient algorithms, poorly designed UI,
  • Over-processing: Unnecessary functions or capabilities. Deliver the solution requested per the scope of the application, no more, no less.
  • Transport: Movement of resources or data. Consider in memory processes versus disk intensive transactions, or client side versus server side data processing.

There is a notable difference between “sloppy programming” and clean code written by someone who knows better. Have you ever spent hours attempting to decipher someone’s code, or even your own? A clean, readable, and well documented file is much easier to work with and, more importantly, understandable.

5S Your Code

We can minimize some forms of waste by using a method known as 5S. IDE’s such as those offered by JetBrains, allow us to create a workspace for a given application, but we can extend this concept to each file or script too.

  1. Sort (Seiri): Eliminate all unnecessary tools, functions, comments, and resources. Choose meaningful file and variable names to minimize tedious and redundant comments in your Code.
  2. Set in Order (Seiton): Use an effective directory management strategy to organize all your files for quick and easy reference. Deploy an effective “Model, View, Controller” strategy when developing your applications. Restrict your functions to a single purpose to better enable re-usability.
  3. Shine (Seiso): Set and follow standardized coding guidelines and naming conventions. Deploy rigorous version control standards.
  4. Standardize (Seiketsu): Publish coding guidelines and maintain your Code accordingly.
  5. Sustain (Shitsuke): Cascade requirements and communicate expectations throughout the organization. Continually review and update the guidelines accordingly.

5S is one of the fundamental elements of Kaizen and, when practiced regularly, helps to minimize the seven wastes, allowing you to work effectively and efficiently.

Until Next Time – STAY lean!

Related Articles and Resources

What is 5S? – kanbanize.com

Give Your Fingers a Break

If everyday typing or keyboarding were a professional sport, we would be more inclined to pay attention to the keyboard at our finger tips. Does the quality of our keyboard affect or influence our efficiency and performance?

Gamers will spend hundreds of dollars on a keyboard to get the slightest edge over their competitors. Look and feel, layout, spacing, travel, shape, switch timing, macro keys, and key combinations can be the difference between winning and losing.

Many of us don’t view activities like programming, writing emails or reports, blogging, and working with spreadsheets, with the same degree of vigour and ambition. I doubt few of us would even begin to appreciate the technology behind one switching mechanism and the advantage it may have over another.

In the beginning …

We used manual typewriters in my grade nine high school typing class. A black ribbon fed between two spools with every long and forceful keystroke. I recall doing numerous exercises to give our “pinky” fingers an extra strength-building workout.

My grade 10 typing class was much more enjoyable as we moved on to electric machines. No more finger workouts, just focus on the task at hand – typing. My fingers could fly effortlessly across the keyboard.

The differences between manual and electric typewriters are many and quite obvious, even to the untrained eye. My typing speed increased dramatically using the electric machine. Achieving 140 words per minute is one of my more memorable achievements.

Just as notable are the physical and ergonomic benefits of the electric typewriter. By the time I finished high school and went on to college, computers began to appear and became an integral part of how we work. Any frustrations or efficiency issues I had with keyboards at the time seemed to all but disappear.

All was good until the IBM PC, and Compaq computers, among others, entered the workplace. Time to choose between the tactile feel of the PC and the soft mushy feel of the Compaq keyboard. Today’s computers and devices have brought a whole new realm of keyboard technologies along with them.

What Keyboards do I Use?

Logitech CRAFT

I use Logitech’s CRAFT keyboard on my home desktop. This is my favourite “third party” keyboard by far, and the flow technology coupled with my MAX Master 2S mouse allows me to switch between three devices on the fly.

Logitech K750

When I’m on the road, I use Logitech’s K750 Solar keyboard. I like the look and feel of this keyboard, USB or Bluetooth connectivity, and I never have to worry about batteries. Yes, I could use the keyboard on my client’s machines, but I prefer to work with my own keyboard and mouse, especially as the cold and flu season approaches.

Logitech K760

Logitech’s K760 solar keyboard is also a frequent traveller, especially when I’m working with multiple Bluetooth devices. While this keyboard lacks the convenience of the extended layout, the smaller footprint makes it easier to take with me where space is a concern. I have been using this keyboard and the K750 pictured above for quite a few years without issue.

Brydge 12. Pro 128

The Brydge 12.3 Pro 128 is the perfect match for my SurfacePro and is almost a twin to my MacBook Pro keyboard. The integrated SSD drive is a welcome added convenience. This keyboard looks great and carries well, although its much thicker than Microsoft’s SurfacePro keyboard offering.

As much as I like the minimalistic design of Microsoft’s SurfacePro keyboard, it is flimsy and tends to bounce if I get too aggressive with my keystrokes. While it’s great for traveling, I prefer something with a more substantial base when I’m working at a desk.

You may be wondering if I’ve ever been pleased with a keyboard that came with the computer. When it comes to laptops, the keyboard is one of the criteria I use in my selection. My 15″ MacBook Pro keyboard is the standard by which all others are measured.

The Republic of Gamers ROG Claymore is my keyboard of choice for gaming though I have very little time to play them. For an added touch of ingenuity, ROG SYNC also has synchronized lighting effects, fan speed control, and CPU temperature monitoring when connected to my ROG laptop.

Does your keyboard matter to you?

We each have our favourites and personal tastes. Some may not even think twice about their keyboard. As for me, I appreciate the experience a quiet, highly responsive, quality keyboard can bring. Of course we must always be mindful of the price we pay for the value we receive.

Whether a performance advantage or efficiency gain exists is subject to opinion. When I consider how many hours my fingers spend typing away at a keyboard, I give them a break and reward them with the best experience I can.

Until Next Time – STAY lean!

Versalytics

On the cutting edge of software

Updating software applications is a routine maintenance practice that many choose to ignore. While many of my applications are set to update automatically, I want to be sure they are current.

I prefer to update my core software packages manually and with good reason. Some updates can be “buggy” and blindly upgrading could shut an application or system down. Code that worked in one version may now refer to deprecated functions or features.

Integrated Development Environments or IDE’s and programming languages continue to evolve. Incorporating and taking advantage of new features and capabilities can make writing code that much more effective and efficient.

I use the suite of IDE’s by JetBrains and appreciate the value my subscription brings to make code that much easier to write and manage. Integrated version control is certainly one of the features that I have learned to depend on over the years.

Performing regular updates also makes it easier to keep up with changes as they occur as opposed to learning them in one sitting several releases later. There is always a risk to being on the cutting – or bleeding – edge of technology, but the rewards may be even greater.

Until Next Time, STAY lean!

Web Browsing with Brave

The Brave web browser addresses two of the most significant concerns when browsing the web: Security and Privacy. With the Brave browser, you don’t have to compromise when surfing the web. Keep your personal data private and secure. As an added bonus, you can save time and even earn rewards to view privacy-respecting ads.

After watching this video by MaxDapp, I was intrigued to give the Brave web browser a try. The browser displays the number of Ads and Trackers that have been blocked, HTTPS Upgrades, and the estimated time you saved by using the Brave browser.

The Brave browser works on mobile too and, depending on your data plan, it can save you money by blocking unwanted ads and trackers. The Brave browser also claims to be 2 to 8 times faster, saving time for things you’d rather be doing other than waiting. Visit the Brave Browser features page for more details.

Like other browsers – including Chrome, Edge, Tor, and Epic, to name a few – the Brave browser is built on Chromium; a free and open-source web browser initially developed by Google. Watch this video by ThioJoe to understand the differences between Chromium and Google’s Chrome browser.

I use a variety of browsers depending on the machine I’m working on. For quick searches, I usually default to using Edge on Windows machines or Safari on my Apple devices. I frequently use Firefox and Chrome across all of my devices as well.

Developing applications for the web typically requires testing across all of the major browsers. When developing code locally on my machine, I use Google’s Chrome browser or Mozilla’s Firefox browser and now the Brave browser has been added to the list.

undefined

After using the Brave browser for just under an hour, I was surprised to learn that 136 Cross-site trackers were blocked. For now, I will continue to use Brave and, instead of sacrificing my privacy and security, hopefully, save a little time and earn some revenue by viewing some ads.

I am not devoted or loyal to merely using one browser. I use the browser that serves my purpose or is most convenient at the time. As I’ve said many times before, “There’s always a better way and more than one solution.” This statement even applies to web browsing.

Visit the Brave Downloads page to get your Brave Browser surf the web with confidence.

Until Next Time – STAY lean!

Versalytics - Logo (293x293)

Related Articles and Resources – Web Browsers:

There are a number of web browsers to choose from and each carries its own unique features and capabilities. The following browsers are listed in alphabetic order and does not reflect any preference or ranking:

Microsoft Excel 2019 VBA and Macros

I received my paperback copy of Microsoft Excel 2019 VBA and Macros, by Bill Jelen and Tracy Syrstad, on December 31, 2018. I finally managed to work my way through it cover to cover and highly recommend this book for anyone wanting to learn how to be more productive with Excel using VBA.

This book presents a wide range of topics where you will learn how to take advantage of VBA and the new features available in Excel 2019. With the assumption that you already know how to use Excel, this book helps you to understand and take advantage of the many capabilities and features of VBA itself to enhance your experience as a user and to create effective and efficient applications.

If you want to increase your productivity with Excel 2019, then I highly recommend getting your copy of Microsoft Excel 2019 VBA and Macros. This is another welcome addition to our growing library of Excel books.

  • ISBN-13: 978-1-5093-0611-4
  • ISBN-10: 1-5093-0611-0
  • Pages: 585

Accompanying Excel workbooks with code, data sets, and bonus macros are available online for download.

Until Next Time – STAY lean!

JIRA Development Cookbook – Third Edition

JIRA Development Cookbook – Third Edition by Jobin Kuruvilla, September 2016, 598 pages. This book is available from Amazon in Paperback and Kindle Edition.

  • ISBN-13: 978-1785885617
  • ISBN-10: 1785885618

Delving into JIRA requires a minimum $10.00 per month expense per user after a free 7 day trial. The question is, does the expense justify the return?

We shall soon find out.

The Logitech CRAFT Keyboard is by far the best keyboard to grace my desktop. Protect your investment with the Hard Travel Case by co2CREA

Until Next Time – STAY lean!

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