• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

JShell? Java 9 looks like NodeJS  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I create classes and stuff with this JShell?
 
Marshal
Posts: 61702
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 4563
286
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 6255
420
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

added:

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.
 
Tim Cooke
Sheriff
Posts: 4563
286
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Liutauras Vilda
Marshal
Posts: 6255
420
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Tim Cooke
Sheriff
Posts: 4563
286
Clojure IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Liutauras Vilda
Marshal
Posts: 6255
420
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Cooke
Sheriff
Posts: 4563
286
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Liutauras Vilda
Marshal
Posts: 6255
420
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Will install JDK9 tonight and will give a try.

I trust you had your spidey sense kicked in and also tried:
string:to_integer("123ABC4").
 
Tim Cooke
Sheriff
Posts: 4563
286
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup.
 
Campbell Ritchie
Marshal
Posts: 61702
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
author
Posts: 232
27
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)

Cheers,

Cay
 
Campbell Ritchie
Marshal
Posts: 61702
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you moved from Eclipse to IntelliJ?

or ..... but jshell works straight “out of the box”.
 
Cay Horstmann
author
Posts: 232
27
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Cheers,

Cay
 
Campbell Ritchie
Marshal
Posts: 61702
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!