JShell is an API and tool that can help compute the values of code snippets. Code snippets must follow the Java Language Specification (JLS) syntax. JShell can also execute queries and commands. The difference between a command and a fragment is that a command must start with a diagonal line, as shown in the following example:
The JShell status imitates a JVM instance. JShell uses the Compiler API (Compiler API) for code analysis, tab code completion, and original code snippet parsing. It uses the Java Debug Interface (JDI) implemented the code replacement function.
If you do not like interactive interfaces, you can also use Batch scripts.
Sample JShell on JavaOneBefore introducing the example on Reinhold JavaOne, I would like to add: if you use the latest Java 9 SDK and enter "java-version, the current version number is changed as follows:
$ java -versionjava version "9-ea"Java(TM) SE Runtime Environment (build 9-ea+136)Java HotSpot(TM) 64-Bit Server VM (build 9-ea+136, mixed mode)
We can compare it with the old format:
java version "1.8.0_91"Java(TM) SE Runtime Environment (build 1.8.0_91-b14)Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
This change is brought about by the new model introduced by JEP 223. The current version of the string is easier to parse and is more in line with the Semantic version (Semantic Versioning) emphasized by current industry practices ). This new pattern helps us identify the major version (major), minor version (minor), or security upgrade Release versions more easily.
In the preceding example, a pre-release identifier ("ea" -- read as "early access") is preceded by a hyphen (-) followed by a hyphen (+, next is the build number (136) corresponding to this build ).
Return to the example. If you enter "jshell" in the command line, you will see the following JShell prompt:
$ jshell| Welcome to JShell -- Version 9-ea| For an introduction type: /help introjshell>
If you enter a simple String declaration like the following, you can see all the callable methods (including the overloaded methods ):
jshell> String x = "foo bar baz"x ==> "foo bar baz"jshell> x.charAt( chars() codePointAt( codePointBefore( codePointCount( codePoints() compareTo( compareToIgnoreCase( concat( contains( contentEquals( endsWith( equals( equalsIgnoreCase( getBytes( getChars( getClass() hashCode() indexOf( intern() isEmpty() lastIndexOf( length() matches( notify() notifyAll() offsetByCodePoints( regionMatches( replace( replaceAll( replaceFirst( split( startsWith( subSequence( substring( toCharArray() toLowerCase( toString() toUpperCase( trim() wait( jshell> x.substring(4,7)$3 ==> "bar"jshell> Arrays.asList(x.split(""))$5 ==> [f, o, o, , b, a, r, , b, a, z]jshell> Arrays.asList(x.split(" "))$6 ==> [foo, bar, baz]
The preceding example shows temporary variables ($3, $5, and $6), which can be used for subsequent expressions if needed.
jshell> import java.util.stream.*jshell> $6.stream().filter(s -> s.startsWith("b")).collect(Collectors.toList())$9 ==> [bar, baz]
In the above example, we imported the "java. util. stream" package. In this way, we can get its method list when prompted by the tab key on the Collectors class.
ConclusionJShell brings a REPL to Java, which is a very useful feature on a classic LISP machine. It helps developers debug code snippets and avoids the complete compilation, running, and debugging process.
From: http://www.linuxeden.com/html/news/20160927/168237.html
Address: http://www.linuxprobe.com/javaone-jshell-power.html