[JAVA] I played loosely with Jshell

Overview

――In this article, I will briefly explain how to use Jshell.

background

--Participating in JJUG CCC the other day --There, Mr. Kishida (@nowokay) was talking about Java 11 in Jshell.

Target

--Jshell beginners --For those who are tired of using Java

What you have prepared

--Mac terminal only --It is assumed that docker is included

Environment

--Just hit the command below

docker run -it openjdk:11 jshell

Then ... image.png

You can use it like this

Let's play for the time being

jshell> String hoge = "hoge"
hoge ==> "hoge"

Hit / vars

――It will tell you the variables you created

jshell> /vars
|    String hoge = "hoge"
|    List<String> list = []
|    String test = "test"

Hit / imports

――It will tell you the one that is currently imported

jshell> /imports
|    import java.io.*
|    import java.math.*
|    import java.net.*
|    import java.nio.file.*
|    import java.util.*
|    import java.util.concurrent.*
|    import java.util.function.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import java.util.stream.*

When you want to import a new one

--Striking import directly

jshell> import java.util.Scanner

As expected, this is not cool, so ...

--At the timing of use, press Shift + tab at the same time and press i immediately after releasing.

jshell> ZonedDateTime
0: Do nothing
1: import: java.time.ZonedDateTime
Choice:
Imported: java.time.ZonedDateTime

Then it will import like this

jshell> ZonedDateTime now = ZonedDateTime.now()
now ==> 2018-12-21T16:14:20.037591Z[Etc/UTC]

Complement

――This is the same as the terminal, and if you press the tab key, it will complete it.

jshell> var lsit = List.
class     copyOf(   of(

You can use it by pressing tab at the same timing (after hitting) as the timing to have it complemented by the IDE like this

new line

――If you made such a list

jshell> var list = List.of("hoge","fuga","piyo")
list ==> [hoge, fuga, piyo]

――For this guy, if you try to write a new line and start a new line at an appropriate place, it will be executed.

jshell> var result = list.stream().filter(word -> word.startsWith("h"))
result ==> java.util.stream.ReferencePipeline$2@31610302

――It's really sad, so ...

jshell> var result = list.stream().filter(word -> word.startsWith("h")
   ...>  ).collect(Collectors.toList())
result ==> [hoge]

You can start a new line by opening () like this, or by returning with. In the previous line.

/exit

――It may not be known unexpectedly, but you can get out with this

jshell> /exit
|  Goodbye

/help

――If you have any problems, please refer to help.

Impressions

--Jshell Pretty fun --There are many parts that rely on IDE completion, and I didn't care much about import classes, so it seems to be a trigger. ――As you may have noticed, I inserted the Java 11 code on the way, but as you can see, it is very good because you can easily try Java 11 on the terminal without having to bother to install the JDK from Oracle. ――I wondered if there are quite a few options in Jshell as a place to try things you want to check (such as time conversion) that do not need to create a Sample project.

Summary

――For the time being, I just played for about an hour, so I haven't enjoyed it so I would like to know if there are any recommended usage scenes or usage commands.

reference

I referred to Mr. Kishida's Qiita article. Learn JDK11 with JShell (2018/12/15 JJUG CCC 2018 Fall)

Recommended Posts

I played loosely with Jshell
I played with Refinements
I implemented Ruby with Ruby (and C) (I played with builtin)
I tried DI with Ruby
I tried UPSERT with PostgreSQL.
I tried BIND with Docker
[Introduction to JSP + Servlet] I played with it for a while ♬
I tried using JOOQ with Gradle
I tried morphological analysis with MeCab
I made a GUI with Swing
I can't install lombok with Gradle.
I tried to interact with Java
I tried UDP communication with Java
What I learned with Java Gold
I tried GraphQL with Spring Boot
I tried Flyway with Spring Boot
What I learned with Java Silver
I tried customizing slim with Scaffold