Forums Register Login
JShell? Java 9 looks like NodeJS
Can I create classes and stuff with this JShell?
(1 like)
Welcome to the Ranch

Haven't you tried it?

I don't use NodeJS, but you can create classes in jshell, yes. It can be awkward to create a class there and later save it elsewhere; you would have to use the /edit facility. But that isn't what it is intended for. It is good for trying out methods and seeing how they work.
(1 like)
The JShell for Java is what is commonly known as a Read-Eval-Print Loop or REPL for short. (pronounced repple, like ripple but starting with a 'rep' instead of a 'rip'). Many other languages provide a REPL dating back decades, so the idea isn't anything new but it is new to Java, and I welcome it wholeheartedly.
(1 like)
I don't think I tried JShell yet, but I'm hoping it stores at least one last statement after return has been pressed.

That's because mainly if you introduce small typo, you'd rather prefer to press UP ARROW or so and get what you typed before you hit enter, so you could fix typo and try execute again, rather than retype everything from scratch.

The lack of this, was the main primarily negative thing against all REPL's I've seen. Or I didn't know how to use them properly.


In general I find them frustrating to use as a learning tool. Because if you write a snippet and have introduced some illegal statements in between, you no longer are able to see a valid snippet in concise way.
Maybe it is just mine experience.
(1 like)
I think the up / down history is a feature of the terminal shell you're using rather than the REPL. Perhaps that's why you didn't get that feature?
(1 like)

Tim Cooke wrote:I think the up / down history is a feature of the terminal shell you're using rather than the REPL. Perhaps that's why you didn't get that feature?

Might be.

Another thing which made my experience frustrating, that again, if you write function and let's say you get 4 lines correct, and then 2 or 3 other attempts were with typos, and 4th and 5th attempts were successful, then you no longer able to see valid function (you managed to come up finally) laid in concise way as you have error messages in between.

But I gave up very quick on other REPL's just after experiencing that. So I'll give a go with JShell once again, but this time with more patience.
Trying to type a single operation over multiple lines in a REPL is very painful. So while technically you could define a class using JShell I expect it would be a mighty unpleasant experience to do so. My most common use of a REPL is for experimenting with using existing functions to see how they behave with particular inputs. It's useful that way because you don't have to write a class with a main method, then compile it, then run it, when all you want to do is find out what Integer.MAX_VALUE is.
(1 like)
Alright, sounds like a bigger part of either gaining or not some success from using REPL is in understanding for what purposes it was created. Make sense now that you said.

So you don't go and don't write your own functions and not trying to use in various scenarios, but rather gaining an understanding how its separate pieces (presumably you about to use in a function) work.
(1 like, 1 cow)
Yes. For example, a couple of weeks ago I was writing a program in Erlang just for fun (because I do this sort of thing for fun) and I was using the 'string:to_integer()' function that I expected to take a string and return and integer. It did not. So how does it work? REPL time.
Whoa there, it returns a tuple of the integer I expected and an empty array. Huh?
Oh right ok, it takes as many characters as it can to make an integer, then returns you what's left as well as the integer. Strings are character arrays so an empty string and an empty array are the same thing.

This all took less than a minute and was much easier than trying to figure it out from within my program.
(1 like)
Thanks. Will install JDK9 tonight and will give a try.

I trust you had your spidey sense kicked in and also tried:
(1 like)
(1 like)

Liutauras Vilda wrote:. . . hoping it stores at least one last statement after return has been pressed. . . . press UP ARROW or so . . . .

Yes, it does. If you have something suitable for a REPL, try jshell. It is really good
(1 like)
I don't think the sweet spot for JShell is to create classes and functions. It can be done, but I don't often find myself doing that. But I frequently add a JAR with some library that I want to learn to the class path, and then construct instances and invoke methods. IntelliJ lets you use a JShell like thing to work with your project's classes, but I couldn't get it to work. (Undoubtedly it will evenutally when I figure out which of the multitude of settings to set just so.)


(1 like)
Have you moved from Eclipse to IntelliJ?

or ..... but jshell works straight “out of the box”.
My students keep telling me to move to IntelliJ, and I give it the good try ever so often, and then I find myself back in Eclipse. I grant that IntelliJ can do some amazing things, but it can also suck up an amazing amount of time when it does the wrong thing and you have to mess with the settings. Maybe one day I'll make the switch for good.


(1 like)
Maybe it is a case of what you have got used to; you obviously know how to find the options you need much faster in Eclipse than IntelliJ.
This parrot is no more. It has ceased to be. Now it's a tiny ad:
ScroogeXHTML 7.2 - RTF to HTML5 / XHTML converter

This thread has been viewed 249 times.

All times above are in ranch (not your local) time.
The current ranch time is
Mar 24, 2018 14:19:18.