10

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?

Naman
  • 27,789
  • 26
  • 218
  • 353
displayname
  • 1,153
  • 1
  • 9
  • 21
  • 1
    I don't think this question is fit for SO and might be closed as too broad. I didn't vote for that, though, because I actually wonder the same thing you do. I always have at least half a dozen IDE instances open, one specifically for playing around with stuff. I see no advantage of using JShell over that, but loads of disadvantages (no build tool support to drop in dependencies, no easy persistence, no full-featured code editor, less overview, lacking support for debugging...). Let's see what other people say. – Nicolai Parlog Oct 28 '17 at 06:42
  • 1
    For the too broad aspect, I do kinda agree this might be closed in general. But I am not sure what brings the comparison between an IDE and a REPL even. They both are supposed to be meant for different purposes in my personal opinion. @Nicolai Plus it spares me of that extra IDE instance consuming way more CPU than the command line instance ;) – Naman Oct 28 '17 at 09:10

1 Answers1

9

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.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • 2
    Thanx a lot for a very detailed answer! I will checkout the links you provided. And I did indeed tried out your code snippets in both JShell and IDE. For me it took approx. same amount of time in both cases. The time-savers with **JShell** were: autosaving everything in a temporary variable, no need to type ';', no need to compile and run manually. Time-savers with **IDE**: very powerful autocomplete and easier code navigation and editing. And in both JShell and IDE I had a fast and easy access to documentation. PS I do understand, that JShell is not suppose to substitute an IDE. – displayname Oct 28 '17 at 09:52
  • 1
    Honestly, this didn't really convince me that such an interpreter has a big advantage over an IDE. Completion triggered via keypress is nothing new, actually, an IDE offers way more powerful tools in this regard, simply because it has a UI dedicated to these tasks rather than settling on a command line console concept. Another advantage of the IDE shows up, once you decide to keep the code of your experiment for further use, as you can simply save with one key shortcut and don't need to remove REPL artifacts from the text, as code and output are already separated. – Holger Oct 28 '17 at 17:51
  • 1
    @Holger Well I didn't intend to portray the IDE vs REPL comparison. As stated in one of the comments, I am not sure what brings the comparison between an IDE and a REPL even. The purposes of both in my opinion are different and hence their use should be opinion based. In terms of saving the experimented code `/save` on the jshell can be used to save the *experimented snippets* as well. – Naman Oct 28 '17 at 18:03