The most user-friendly explanation of the new features of Java 10

Since the beginning of Java 9, Oracle has adjusted the release strategy of the Java version. Instead of a large version in the previous n years, Oracle has replaced a small version in 6 months and a large version in three years. In this way, the latest changes of Java can be launched quickly. The maintenance cycle of the small version is shortened to that before the release of the next version, and the maintenance cycle of the large version is as long as 3 years. 10 is such a small version. Because subsequent versions of Java will basically include previous new features, it is better to write the changes brought by Java 10 separately.

1. JEP 322 - time based version number

As mentioned above, Java has adjusted the release strategy. In order to adapt to this release rhythm, the recording method of java version number has changed.

The new mode of version number is: $feature$ INTERIM.$ UPDATE.$ PATCH

View your own version of Java 10.

$ java -version
java version "10.0.1" 2018-04-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10,mixed mode)

2. JEP 286 - local type inference

The JEP 286 proposal allows java to add local type inference (local variable type inference) function, which allows java to automatically infer data types like VaR in JS or auto in other languages. In fact, this is just a new syntax sugar. The underlying layer has not changed. VAR has been converted into specific data types at compile time, but this can reduce the writing of code.

You can use VaR syntax like this.

var hashMap = new HashMap<String,String>();
hashMap.put("微信","wn8398");
var string = "hello java 10";
var stream = Stream.of(1,2,3,4);
var list = new ArrayList<String>();

If you decompile the compiled code, you will find that it is still a familiar code fragment.

HashMap<String,String> hashMap = new HashMap();
hashMap.put("微信","wn8398");
String string = "hello java 10";
Stream<Integer> stream = Stream.of(1,4);
ArrayList<String> list = new ArrayList();

Var seems to be easy to use, but there are also many restrictions. The official introduced that VaR can only be used in the following situations.

Three usage scenarios are shown below.

public static void testVar() {
    // 情况1,没有初始化会报错
    // var list;
    var list = List.of(1,4);
    // 情况2
    for (var integer : list) {
        System.out.println(integer);
    }
    // 情况3
    for (var i = 0; i < list.size(); i++) {
        System.out.println(list.get(i));
    }
}

Although there are many restrictions on the use scenario of VaR, you should pay attention to it in actual use. Like the following code, you may not be able to see the data type of result at a glance.

var query = "xxx";
var result = dbUtil.executeQuery(query);

3. JEP 317 - Java based JIT compiler (experimental)

This function enables the JIT compiler Graal developed based on Java to be used on Linux / x64 platform in combination with Java 10. It is an experimental JIT compiler. Some people say that it is also the most futuristic introduction in Java 10. In fact, Graal has been introduced in Java 9. It brings AOT (ahead of time) compilation in Java and supports a variety of languages, such as JS, python, ruby, R, and other languages based on JVM (such as Java and kotlin) and llvm (such as C and C + +).

To switch to Graal, you can use the following JVM parameters.

-XX:+UnlockExperimentalVMOptions -XX:+UseJVMCICompiler

There's one thing I think is very interesting. Look at this picture.

This is very interesting. Graal is written in the Java language, a compiler written in Java, and then used to compile Java bytecode into machine code.

Graal official website: https://www.graalvm.org/

4. JEP 310 - class data sharing

When the JVM starts, one step is to load classes in memory. If there are multiple jars, the speed of loading the first jar is the slowest. This prolongs the startup time of the program. In order to reduce this time, Java 10 introduces the application class data sharing (CDS) mechanism, which can share the classes you want to share between programs and enable different Java processes to share this class to reduce the space occupied by this class and loading speed.

5. JEP 307 - G1 parallel full GC

G1 garbage collector has been introduced as early as Java 9. G1 has many advantages. In Java 10, a small adjustment is made. When the G1 concurrent collection thread cannot quickly complete the full GC, it will automatically switch to parallel collection, which can reduce the GC speed in the worst case.

6. JEP 314 - Unicode language label extension

This proposal enables JDK to implement more extensions specified in the latest ldml specification.

The following extension methods are mainly added.

java.time.temporal.WeekFields::of
java.util.Calendar::{getFirstDayOfWeek,getMinimalDaysInWeek}
java.util.Currency::getInstance
java.util.Locale::getDisplayName
java.util.spi.LocaleNameProvider
java.text.DateFormat::get*Instance
java.text.DateFormatSymbols::getInstance
java.text.DecimalFormatSymbols::getInstance
java.text.NumberFormat::get*Instance
java.time.format.DateTimeFormatter::localizedBy
java.time.format.DateTimeFormatterBuilder::getLocalizedDateTimePattern
java.time.format.DecimalStyle::of

Give it a try.

Currency chinaCurrency = Currency.getInstance(Locale.CHINA);
Currency usCurrency = Currency.getInstance(Locale.US);
System.out.println("本地货币:" + chinaCurrency);
System.out.println("US.货币:" + usCurrency);

String displayName = Locale.getDefault().getDisplayName();
String displayLanguage = Locale.getDefault().getDisplayLanguage();
String displayCountry = Locale.getDefault().getDisplayCountry();
System.out.println("本地名称:" + displayName);
System.out.println("本地语言:" + displayLanguage);
System.out.println("本地国家:" + displayCountry);
int firstDayOfWeek = Calendar.getInstance().getFirstDayOfWeek();
System.out.println("本地每周第一天:" + firstDayOfWeek);

Output results.

本地货币:CNY
US.货币:USD
本地名称:中文 (中国)
本地语言:中文
本地国家:中国
本地每周第一天:1

7. API update

Java 10 removes some APIs and adds some practical methods. For example, you can use collection Copyof copies an immutable set, which will not be affected even if the original set elements are changed.

var list = new ArrayList<String>();
list.add("wechat");
list.add("wn8398");
List<String> copyList = List.copyOf(list);
list.add("test");
System.out.println(copyList);
// result
// [wechat,wn8398]

A new method orelsethrow is also added for optional. Calling this method can also get the value in optional, but if the value is null, an exception will be thrown.

In addition, when the stream finally collects data, collectors can directly specify the collected collection as an immutable collection, as shown below.

list.stream().collect(Collectors.toUnmodifiableList());
list.stream().collect(Collectors.toUnmodifiableSet());

Other updates

The update content of Java 10 is more than that. The above just lists common and interesting new features. There are also some updates, such as:

All articles and cases have been uploaded to GitHub: niumoo / JDK feature < end > this article is an original article by Darcy. If you love it, you can pay attention to the official account, and constantly push the original articles to share with you.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>