The--jshell of new characteristics of JAVA9

Source: Internet
Author: User
Tags modifier

JAVA9 has been officially released in Beijing time on September 22, and developers can download the latest jdk9 from Oracle JDK online. The new features in Jdk9 and Jdk8 are different: stream and lambda expressions in jdk8 allow developers to feel the new version of functionality very quickly and intuitively, while the key update in JAVA9-modularity, does not give developers an intuitive feel.

The first thing to bring today is the intuitive new features in java9: Jshell. An introduction to more JDK9 features can be found in Infoq's article:Https://mp.weixin.qq.com/s/Onj9ZJYbV5pLTAPcYBJREA

For more information on new features in JDK9, please refer to:https://docs.oracle.com/javase/9/whatsnew/toc.htm# Jsnew-guid-c23afd78-c777-460b-8ace-58be5ea681f6

Jshell Target

The goal of Jshell is to provide an interactive tool through which to run and calculate expressions in Java. Developers can easily interact with Jshell, including: Edit history, Tab code completion, auto-add semicolons, configurable imports and definitions. Many other mainstream programming languages, such as Python, have already provided the console, which makes it easy to write some simple code for testing. It is worth mentioning that Jshell does not provide a new interactive language, all code written in Jshell must conform to the Java language Specification, graphical interface and debugging support is not, Jshell one goal is to be able to use Jshell interaction in the IDE, Instead of implementing the IDE's implementation capabilities.

The first exercise in every programming language is to print "Hello,world", and with Jshell, Java developers are finally not writing a class and writing "strange" main methods, which I believe is a boon for beginners.

Use of Jshell

After installing JDK9 and configuring environment variables, you can use Jshell in the console, Jshell in the/bin directory of the Java installation directory:

More commands can be viewed using/help, for example:

/exit: Exit Jshell

/list: Viewing an expression that has already been output

It is worth mentioning that using the TAB key to activate the code hints, again using the TAB key to see the function's document, do not have to use ";" at the end of the expression, Jshell will automatically add a semicolon to the expression.

Jshell Descriptionfunction

The code is entered as a code snippet (snippets), and the code snippet needs to conform to the Java syntax rules:

    • Expression

    • Statement

    • ClassDeclaration

    • Interfacedeclaration

    • MethodDeclaration

    • Fielddeclaration

    • Importdeclaration

For example, you can then initialize the variable in Jshell:

int a = 42;

You can also define classes:

 Public class c{  publicvoid Fun () {    System.out.println ("function");}  }
rules

All code fragments, such as class definitions, method definitions, have a name associated with it, and other blocks of code can refer to it by this name. The code snippet defined follows these rules:

    • Access control adornments (private/protected/public) are ignored. All defined code fragments can be accessed by other code snippets.

    • The final modifier is ignored.

    • The static modifier is ignored because there is no user-visible class that contains static methods, and these wrappers are made by Jshell.

    • The default and synchronized modifiers are not allowed.

    • The abstract modifier is only allowed for decorated classes.

Many code snippets contain nested definitions, such as defining member variables and member methods in a class. The function defined in the member function uses an access control character that is valid. That is, if you define a method in class C private void Fun (), you cannot access it through new C (). Fun () in another code fragment.

forward reference (Forward reference)

In Java classes, the methods defined in subsequent code can be used, which is also possible in Jshell (for example, you can call Method B () later defined in Method A ()). However, the initialization of a variable does not support forward referencing.

Modular Environment Configuration

The modular environment can be configured and added directly to the compilation and run options. The compile option uses-C, and the Run option uses-R. The new features related to Jigsaw and modularity are followed by a further elaboration.

Summary

Jshell provides a console interface that allows developers to quickly write code snippets and run them to view the library's documentation and write simple programs. In scenarios where fast validation is required, we no longer need to define classes, redefine methods, define main () methods, or unit tests to validate our ideas; for Java beginners, this also provides an easy way to learn Java, which is a simple and practical feature.

Jshell Related Documents

The Jshell module API specifications can found here:

    • Http://download.java.net/java/jdk9/docs/api/jdk.jshell-summary.html

which includes the primary Jshell API (package jdk.jshell ) Specification:

    • Http://download.java.net/java/jdk9/docs/api/jdk/jshell/package-summary.html

The jshell tool Reference:

    • Https://docs.oracle.com/javase/9/tools/jshell.htm

is part of the Java Platform, Standard Edition Tools Reference:

    • https://docs.oracle.com/javase/9/tools/tools-and-command-reference.htm

The--jshell of new characteristics of JAVA9

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.