JavaOne 2016--audience to see the power of Jshell

Source: Internet
Author: User

Guide In the keynote speech of JavaOne 2016, the chief architect of the Java Platform Group, Mark Reinhold, pointed out that Java 9 was not just Jigsaw, but included 85 Jep for Java 9. I'm here to focus on a new Java feature he emphasizes, JEP 222, which is the Java shell (also known as Jshell).

The Jshell,java 9 enables developers to use REPL (Read-eval-print loop), an interactive tool that calculates the user's input and prints the output, either as a value or as a state change.

What is Jshell?

650) this.width=650; "class=" Alignnone wp-image-33630 size-full "src=" http://www.linuxprobe.com/wp-content/uploads/ 2016/09/javaone-jshell-power.jpg "alt=" Javaone-jshell-power "height=" width= "520"/>jshell is an API and tool, It can help calculate the value of a code fragment. Code snippets must follow the syntax of the Java language Specification (Java Language SPECIFICATION,JLS). Jshell can also execute queries and commands. The difference between a command and a fragment is that the command starts with a slash and can refer to 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 state actually mimics a JVM instance. Jshell uses the compiler API (Compiler API) for code Analysis, tab code completion, and parsing of the original snippet, which implements the code substitution functionality using the Java Debug Interface (Java Debug Interface,jdi). You can also use bulk scripting if the user doesn't like the interactive interface.

Examples of Jshell shown on JavaOne

650) this.width=650; "class=" Alignnone wp-image-33632 size-full "src=" http://www.linuxprobe.com/wp-content/uploads/ 2016/09/javaone-jshell-power-1.jpg "alt=" javaone-jshell-power-1 "height=" width= "391"/> In introduction Reinhold JavaOne on the sample, I would like to add that if you use the latest version of the Java 9 SDK and enter "Java–version", then you will find that the current version number string will be 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 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 string is easier to parse and more consistent with the semantic version (Semantic Versioning) that is highlighted in current industry practice. This new model can help us to more easily identify the release version of the major version (major), iteration (minor) or security upgrade. In the example above, the pre-release identifier ("EA"-read as "Early access") will have a "-" in front of it, followed by a "+" sign followed by the build number (136) corresponding to the build. Back to the sample, if you enter "Jshell" at the command line, you will see the Jshell hint as follows:

$ jshell|  Welcome to Jshell--Version 9-ea| For an introduction type:/help introjshell>

If you enter a simple string declaration like the following, you will see all the callable methods (including 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 ()   &nbsP;            matches (                notify ()                 notifyall ()              offsetbycodepoints (    regionmatches (          replace (                replaceall (             Replacefirst (          split (                  startswith (             subsequence (            substring (&NBSp;            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 example above shows temporary variables ($ $, $ $, and $6), which can be used for subsequent expression calculations if needed.

Jshell> import java.util.stream.*jshell> $6.stream (). Filter (S-s.startswith ("B")). Collect ( Collectors.tolist ()) $9 ==> [Bar, Baz]

In the example above, we have imported the "Java.util.stream" package, so that when you tab on the Collectors class, you can get a list of its methods.

Conclusion

Jshell brings Repl to Java, which is a very useful feature on a classic Lisp machine. It helps developers debug snippets of code, avoiding complete compilation, run, and debug processes.

Originally from: http://www.linuxprobe.com/javaone-jshell-power.html


This article is from the "Linux self-scholar" blog, so be sure to keep this source http://luckyone.blog.51cto.com/7263520/1859068

JavaOne 2016--audience to see the power of Jshell

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.