JAVA9 new feature -5-java REPL tool: Jshell command

Source: Internet
Author: User

1. Official feature

222:jshell:the Java Shell (read-eval-print Loop)

2. Creating a background

Languages such as Python and Scala have long had an interactive programming environment REPL (READ-EVALUATE-PRINT-LOOP), which evaluates statements and expressions in an interactive manner. Developers only need to enter some code, they can get feedback on the program before compiling. For the previous Java version to execute the code, you must create the file, declare the class, and provide a test method to implement.

3. Design concept

Fast and easy to write and run

4. Achieving the goal

Java 9 finally has the REPL tool: Jshell. Use Jshell to declare variables directly, evaluate expressions, and execute statements without creating a class. That is, you can run Java code directly on the command line at development time without having to create a Java file, without having to explain the "public static void Main (string[] args)" nonsense.

Jshell can also load statements from a file or save statements to a file.

Jshell can also be the TAB key for auto-completion and auto-add semicolons.

5. Use examples to recall Jshell

Get help

Basic use

Tips: In a Jshell environment, the ";" at the end of the statement is optional. But the recommendation is best added. Improve code readability.

Import the specified package

The default has been to import all the following packages: (including Java.lang packages)

Simply press the TAB key to automatically complete the code

Lists all valid code snippets in the current session

View all created variables under the current session

View all the methods that have been created under the current session

Tips: We can also redefine the method of the same method name and parameter list, which is the modification (or overwrite) of the existing method.

Using the external code Editor to write Java code

Loading source code from an external file

The Helloworld.java file is available under the specified directory:

Call with the/open command:

No exception was inspected (compile-time exception)

Description: It was supposed to force us to capture a ioexception, but it didn't appear. Because Jshell in the background for us hidden.

Exit Jshell



Still Silicon Valley interviewer Song Hongkang
Links: https://www.jianshu.com/p/3f8b1783b1d1
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

JAVA9 new feature -5-java REPL tool: Jshell command

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.