Java 9 is faster: JShell hands-on practice

Source: Internet
Author: User

Java 9 is faster: JShell hands-on practice

HowIs it one of the coolest features of Java 9?

Last week, I finally took the time to experience a preemptive version of Java 9. The first stop is JShell, which is also called the Kulla project. This is why at first I had the courage to try Java 9 first. That's right. The official Java 9 release date is currently set to September 2016, but the first trial version has a full-function JShell.

New article: Java 9 faster: JShell programming practices -- # Java REPL http://t.co/F8cQ9X5dQM pic.twitter.com/JoWR6i8PSz

-- Takipi (@ takipid) July 7, 2015

Interesting fact 1:The Kulla project is the internal name of the JShell R & D team. Kulla is the god of architects in the mythical masodo-imya, and he was evicted after the construction. This implies that Kulla's use case) is a learning tool?

So what is JShell?

JShell/Kulla is the first official Java REPL Read-Eval-Print-Loop (Interactive Programming Environment), a command line tool. It allows you to execute Java statements without using classes or method packaging. It is similar to the Python interpreter, or other JVM languages that support REPL locally, such as Scala and Groovy. In the coming features of Java 9, this is definitely one of the more interesting features. Except for the Jigsaw project, which has more interesting JDK features coming to your side !) You can read our previous five special articles on Java 9, which can change your encoding method. So far, if you want to use REPL in Java, you can also use alternative solutions, such as repl. it with multi-language support or unofficial Java REPL projects. However, when Java 9 is used, REPL and langtools packages are directly bound to JDK.

Interesting fact 2:The other mythical OpenJDK project is Valhalla. Its goal is to go beyond Java 9 and focus on the development of the characteristics of the numeric type and generics. Not good.

To put it bluntly, let's run it and see what it can do. In the short terminal recording video below, we run JShell v0.61 to complete a quick test. There is a known problem in OSX 10.10.3, that is, the prompt is mixed with some output. You don't need to care about it. It will be repaired as soon as possible in the future ).

NOTE: For the JShell video, see the original article)

Let's review what we just saw: some of the most interesting features of JShell

[8/7/2015] update: edited in Brian Goetz's note. He is a Java architect of Oracle.

1. The semicolon is optional for pure statements.

Hello JShell! Based on REPL's fast and flexible features, we can relax without adding a number at the end of the statement: Hello JShell!

There is no semicolon here.

In fact, in our investigation into future Java features, this feature is recognized by most people. Of course, the semicolon is retained, whether as a terminator or a separator. REPL allows you to type pure expressions and statements at a time. Therefore, semicolons are optional for JShell Terminal Use Cases.

2. REPL Network

When using JShell, we are not limited by machine and network access, which brings interesting opportunities. For example, think about it as a terminal to communicate with the server, remotely connect to the server and control some parameters from outside. Another option is to query the database. There is no limit to this.

A simple example of getting an HTTP request header using JShell

3. Help! Save and load a workspace

In addition to running and computing Java statements, JShell supports a series of command lines to help you understand and browse the REPL environment. You can save and load the workspace, which makes Java a bit like a scripting language ), after all the code is executed, you can return it at the command line prompt. At this time, you can see the OS x bug I mentioned in the terminal video ).

Other useful command lines list all currently available variables and methods, and display all your historical commands, listing all the code you have written so far is sometimes a little strange ).

This is a complete list of currently available commands:

JShell/help

4. No exception Detected

If you are always worried that the checked exception will destroy your REPL experience-you don't have to worry about it. The JShell is hidden in the background for you. In the following example, we should force us to capture an IOException, but it does not appear. The following example shows how to read and print a file without IOException.

In one case, a check exception pops up, that is, when we try to run a Thread and use the Thread. sleep () Statement in it. Because this is an overall method rather than a separate pure statement, it must be a fully valid Java statement:

Let's continue to look at some other features that are not reflected in the terminal video.

5. Java expressions

The JShell terminal can also calculate Java expressions by itself. String connection, method callback, algorithms, and so on. Basically, anything you can wrap in System. out. println (/Expression here. As you may already know about other computing methods, it immediately assigns the result to its own variable and prints it out.

6. Forward reference

JShell provides great support for forward references, so you can reference other methods or variables when defining methods, and these methods or variables will only be defined after a while. This is an example of the REPL Guide provided by AdoptOpenJDK:

7. JShell API

In addition to being a separate JDK tool, JShell also provides an API that provides all functions for external calls. This means that external tools can connect to and use the JShell function, so some interesting possibilities are suddenly developed, such as referencing it in Eclipse, IntelliJ, or Netbeans IDE. There may be more creative use cases for this API, such as using this API to expand static analysis tools, automated testing, or even connecting to Real-Time JVM.

8. Modify the definition during running

You can redeclare variables, methods, or classes without worrying about the original declaration. Once you perform this operation, you will receive a message indicating what happened, but everything else remains the same.

Egg: All you need is love.

If you look for the directory/repl/samples, you will find this cute sample made using Swing, and draw three hearts for you:

Java <3 REPL

How do you start using JShell?

Before you start, read the Adopt OpenJDK documentation: LJC, London Java Community. The following is a quick summary of the steps to help you start smoothly. Especially if you have never played a Java preemptive version before.

1. install Java 9 and try JDK first
2. Compile the kulla-dev Branch

1

2

3

4

5

hg clone http://hg.openjdk.java.net/kulla/dev kulla-dev

cd kulla-dev

bash configure –with-boot-jdk=/path/to/jdk1.8.0

make clean images

make install <=== optional

3. Compile REPL

1

2

cd langtools/repl

bash ./scripts/compile.sh

4. Then ...... Now you can prepare to run

1

bash ./scripts/run.sh

Important tips for OS X users:Before Running compile. sh & run. sh, edit these scripts and run #! /Usr/bin/sh #! /Bin/sh.

If you have any questions about this process, please submit it to the comment area below and we will try our best to help you.

Summary

We are very excited to see that the Kulla project is becoming increasingly mature and will eventually be fully functional. JShell has a wide range of use cases, but the first thing is to help new and existing developers quickly test their new skills. The basic Java or new classes only enable you to enrich your brain. In addition, during the current period, JShell has placed Java on the same level as other languages that have these features. In the future, JShell may be able to provide an official API and a smooth command line experience, create new ideas.

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.