Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why and how do you use JShell?

I went through a couple of tutorials for JShell and I still do not understand the most important part: why would I even use JShell?

Here what Oracle says:

"You can test individual statements, try out different variations of a method, and experiment with unfamiliar APIs within the JShell session".

Yes, great, but for all those things I can just use an IDE. I do understand, that REPL should make code evaluation faster. However, testing a snippet of code in IDE in a dummy Hello World project with Sysout is for sure not slower?

In fact, IDEs provide autocomplete, detect errors early and I can checkout an implementation of a method/class with a mouse click. This all should make code testing in IDE faster than in JShell?

What am I missing? Can someone give me a couple of use cases, where using JShell is better then using IDE? How do you guys use JShell?

like image 921
displayname Avatar asked Oct 27 '17 18:10

displayname


People also ask

What is the purpose the implementation of JShell in Java 9?

JShell is a new concept introduced in Java 9 version. It provides Java with REPL (Read-Eval-Print-Loop) ability. By using JShell, we can test the java-based logic and expressions without compiling it. REPL acts as an immediate feedback loop and has a great effect on productivity in that particular language.

How do I use JShell in terminal?

To start Jshell, first we must have installed Java 9 then open terminal in Linux or command prompt in windows and type jshell. It will start jshell session and displays a welcome message to the console. To display a simple “Hello Java” message, write print command without creating class and hit enter.

How do I run a JShell?

To start JShell, enter the jshell command on the command line. JDK 9 or higher must be installed on your system. If your path doesn't include the bin directory, for example java-home/jdk-9/bin , then start the tool from within that directory.

What are JShell commands?

JShell commands control the environment and display information within a session. Notice that the types and values of variables and the type signature of methods are displayed. The default startup script consists of several common imports. You can personalize your startup entries with the /set start command.


1 Answers

Though I would really encourage you to go ahead and read The Java Shell motivation and goals.

Yet to answer your question with a slight hands-on and I would actually be glad to know about the time you take and procedure you would follow while trying this out on an IDE. So, with few those, here you go:-

1. I recently got to know that Java 9 introduced overloaded Convenience Factory Methods for Collections and then was curious about what do they do and how do I make use of them.

Steps

-> Type 'jshell' on command prompt (assuming JAVA_HOME is set to JDK9's bin)
-> Type 'List.o'
-> Press Tab (completes the `of`) 
-> Tab displays all signatures 
-> Tab starts reading the documentation from first

enter image description here

2. How about persisting the syntax of the code? If you would have noticed in the above image the initialization of the List didn't even bother to care about the variable to store it in, terminating semi-colon etc.

3. How quickly can you find out the exceptions thrown by some piece of code that you are about to implement? So, a use case here, I want to create a Map of String(name of fruits) to String(their color), then add some more fruits to it in the later phase and leave out those colors which I am not certain about. Then finally get a list of all those fruits which are Red. And of course, since I am learning Java 9 these days, I would try to use as much of APIs as from Java9.

enter image description here

And while you would try adding all of that(^^) code in your IDE, you can yourself notice the time you would eventually take to realize all those characteristics of Immutable Map Static Factory Methods.

PS:

  • Robert has presented Jshell here which is a detailed view of how all can the JShell be effective.

  • As you do all of the above, do keep in mind that it's not a goal of Jshell to be an IDE.

  • And all of that, in general, does simple experimentation with a language by bypassing the compile stage of the "code -> compile -> execute" cycle. read-eval-print-loop

like image 81
Naman Avatar answered Sep 22 '22 05:09

Naman