JavaOne 2016-the audience can see the power of JShell

Source: Internet
Author: User

JavaOne 2016-the audience can see the power of JShell
GuideIn the keynote speech of JavaOne 2016, Mark Reinhold, chief architect of the Java platform group, pointed out that Java 9 is not just Jigsaw, but Java 9 contains 85 JEP in total. I will pay attention to a new Java feature he emphasized here, JEP 222, that is, Java shell (also known as JShell ).

With JShell, Java 9 enables developers to use REPL (Read-Eval-Print loop), an interactive tool that computes user input and prints output, the output content is either a value or a state change.

What is JShell?

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:

jshell> /import|    import java.util.*|    import java.io.*|    import java.math.*|    import java.net.*|    import java.util.concurrent.*|    import java.util.prefs.*|    import java.util.regex.*

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 JavaOne

Before 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.

Conclusion

JShell 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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.