Foreword

java After the storm, it has been in existence for more than 30 years since its birth in 1995! There are a total of 1-10 10 large versions of Java. Of course, java10 is still in the stage of fixing bugs. It has not been officially released. The first official basic usable version is 3.20 in 2018. Let us look forward to it together! 🙂

The java10 schedule shown below:

Environmental Preparation

In order to quickly experience the java10, select [docker] (https://blog.zhouzhipeng.com/tag/docker) environment is the best (do not install [docker] (https://blog.zhouzhipeng.com /tag/docker) Please refer to my other article for the environment: [Introduction to the world of docker] (https://blog.zhouzhipeng.com/walk-in-docker-beginning.html)). Take a look at the docker hub and see if there is any java10 tag.

Https://hub.docker.com/_/openjdk/

As shown above, it really is!!)

Ok, then don’t talk nonsense, let’s run and see!

Running the Docker version of the Java10 container

Use the following command to run and enter the inside of the container:

Docker run -it --rm openjdk:10 bash

The result is shown in the figure: (The docker run command will first download the openjdk image)

After entering the container, first use java -version to see the version:

Root@4189190b4a78:/# java -version
Openjdk version "10"
OpenJDK Runtime Environment (build 10+32-Debian-1)
OpenJDK 64-Bit Server VM (build 10+32-Debian-1, mixed mode)

You can see that it is indeed the java10 version, so let’s experience the new features one by one!

According to the openjdk official website description (http://openjdk.java.net/projects/jdk/10/):

286: [Local-Variable Type Inference] (http://openjdk.java.net/jeps/286)
296: [Consolidate the JDK Forest into a Single Repository] (http://openjdk.java.net/jeps/296)
304: [Garbage-Collector Interface] (http://openjdk.java.net/jeps/304)
307: [Parallel Full GC for G1] (http://openjdk.java.net/jeps/307)
310: [Application Class-Data Sharing] (http://openjdk.java.net/jeps/310)
312: [Thread-Local Handshakes] (http://openjdk.java.net/jeps/312)
313: [Remove the Native-Header Generation Tool (javah)] (http://openjdk.java.net/jeps/313)
314: [Additional Unicode Language-Tag Extensions] (http://openjdk.java.net/jeps/314)
316: [Heap Allocation on Alternative Memory Devices] (http://openjdk.java.net/jeps/316)
317: [Experimental Java-Based JIT Compiler] (http://openjdk.java.net/jeps/317)
319: [Root Certificates] (http://openjdk.java.net/jeps/319)
322: [Time-Based Release Versioning] (http://openjdk.java.net/jeps/322)

Here we pick a few to see and experience it first!

286. Local variable type inference

Specific feature description: http://openjdk.java.net/jeps/286

Yes, you can now define your variables like this!

Var list = new ArrayList<String>(); // The list variable is inferred to be ArrayList<String>
Var stream = list.stream(); // stream object is inferred to be Stream<String>

Finally, java can also define variables as simple as other dynamic languages. Before comparing java10, you can only write:

ArrayList<String> list = new ArrayList<String>(); // The list variable is inferred to be ArrayList<String>
Stream<String> stream = list.stream();

I don’t know how to practice fake! Try using jshell to knock two lines:

Root@cc23e6db2879:/# jshell
| Welcome to JShell -- Version 10
| For an introduction type: /help intro

Jshell> var a=1
a ==> 1

Look at the type of a, here use the instanceof keyword (although the basic type will report an error with this judgment, but it can also reflect its type)

Jshell> a instanceof Integer
| Error:
| unexpected type
| required: reference
| found: int
| a instanceof Integer
| ^

Note the above error message found : int Description a has indeed been inferred to be an int type.

Look at the variable declaration of type String:

Jshell> var str=new String("zhouzhipeng.com");
Str ==> "zhouzhipeng.com"

Jshell> str instanceof String
$2 ==> true

Looked good, but the actual identifier var is not a keyword!

The identifier var is not a keyword; instead it is a reserved type name or a context-sensitive keyword. This means that code that uses var as a variable, method, or package name will not be affected; code that uses var as a Class or interface name will be affected (but these names are rare in practice, since they violate usual naming conventions).

The meaning of translation is:

The identifier var is not a keyword, but a reserved type name or a context sensitive keyword. This means that code that uses var as a variable, method, or package name will not be affected; code that uses var as the class or interface name will be affected (but these names are rare in practice because they violate the usual naming conventions) ).

So, we declare that a variable can also be compiled normally:

Jshell> var var ="zhouzhipeng.com";
Var ==> "zhouzhipeng.com"

But it is not possible to declare a class like this:

Jshell> class var{}
| Error:
| 'var' not allowed here
| as of release 10, 'var' is a restricted local variable type and cannot be used for type declarations
| class var{}
| ^

Usage restrictions

Of course, in addition to var there are some other restrictions on use, quickly over:

  1. There must be an initial value when declaring a variable with var
Jshell> var x;
| Error:
| cannot infer type for local variable x
| (cannot use 'var' on variable without initializer)
| var x;
| ^----^
  1. var declaration must have an explicit target type, such as can not be used in the lamdba variable or array variable
Jshell> var f = () -> { };
| Error:
| cannot infer type for local variable f
(lambda expression needs an explicit target-type)
| var f = () -> { };
| ^----------------^

Jshell> var k = { 1 , 2 };
| Error:
| cannot infer type for local variable k
(array initializer needs an explicit target-type)
| var k = { 1 , 2 };
| ^----------------^

The initial value of the variable declared by `var cannot be null

Jshell> var g = null;
| Error:
| cannot infer type for local variable g
| (variable initializer is 'null')
| var g = null;
| ^-----------^
  1. var cannot declare non-representable types, including: null type, cross type (concept reference: https://www.w3cschool.cn/java/java-intersection-type.html) and anonymous class type

Description:

This piece is rather embarrassing and less common. It is not convenient to explain too much before I can figure out it so as not to mislead everyone. For details, please refer to the description of the "unrepresentable" type in the original text:

 Http://openjdk.java.net/jeps/286 Articles are located at the "Non-denotable types" description

Conclusion

The above is about the new features of java10: the experience and description of the local variable type inference, more features please continue to follow this blog blog.zhouzhipeng.com

References
http://openjdk.java.net/projects/jdk/10/
https://hub.docker.com/_/openjdk/

Last modified: 2019年3月29日

Author

Comments

Write a Reply or Comment

Your email address will not be published.