New features of each version of the talk after Java8

[ This is the first 11 ZY original technology articles ]

One day wandering the Internet, suddenly saw an article describes the new features of Java article 11, suddenly was surprised, after all, I know for a version of Java remains at the Java 8, while the syntax and API is still in daily use Java 7 on. So take the time looked at the characteristics of each version after 8 Java, made a summary.

Article Overview

summary

JDK

JDK stands for Java Development Kit, is a Java development environment. We usually refer to JDK refers to the Java SE (Standard Edition) Development Kit. In addition to Java EE (Enterprise Edition) and Java ME (Micro Edition platforms).

Java release cycle

version release time name
JDK Beta 1995 WebRunner
JDK 1.0 1996.1 Oak
JDK 1.1 1997.2
J2SE 1.2 1998.12 Playground
J2SE 1.3 2000.5 Kestrel
J2SE 1.4 2002.2 Merlin
J2SE 5.0 2004.9 Tiger
Java SE 6 2006.12 Mustang
Java SE 7 2011.7 Dolphin
8 (Lats) Java 2014.3
Java SE 9 2017.9
Java SE 10 (18.3) 2018.3
11 Java (18.9 LTS) 2018.9
Java SE 12 (19.3) 2019.3
Java SE 13 (19.9) 2019.9

Let's look at some of the Java development process an important node.
In 1995 alpha and beta Java public release, named WebRunner.

1996.1.23 Java first version, named the Oak. But the first stable version of JDK 1.0.2, known as the Java 1 .

1998.12.8 released J2SE 1.2. This version of J2SE 5.0 was renamed to the Java 2 . Wherein the SE refers Standard Edition, in order to distinguish J2EE (Enterprise Edition) and J2ME (Micro Edition).

2000.5 released J2SE 1.3, which contains the HotSpot the JVM . The HotSpot JVM was first released in 1999.4, called J2SE 1.2 JVM.

2004.9.30 released J2SE 5.0 . Why is this version of the name and the first few versions are not the same? This version of the originally planned 1.5 named to adopt the old nomenclature. However, in order to better reflect the maturity of this version, it changed its name to 5.0.
After this version, with a new version control system, used to represent the 5.0 version of the product, used to represent the stable version of J2SE, and used to represent the developer version 1.5.0, which is Java 5.0 = JDK 1.5.0.

2006.12.11, J2SE renamed the SE the Java , version 0.01 removed. After corresponding version is Java 6 = JDK 1.6, Java 7 = JDK 1.7.

2011.7.7. Released Java SE 7, it is a major version update. Updated many features.

2018.3 release Java SE 10. Before that, Java is basically a two-year version, in addition to Java SE 7 after five years, Java SE 8 after three years. After that, it is every six months, released a new version . But not every version is LTS (Long-Term-Support) . According to Oracle's plan, every three years there will be a LTS version. LTS is the most recent version of the Java SE 11.

OpenJDK VS Oracle JDK

OpenJDK in 2007 released by the Sun Corporation (now Oracle Corporation) is. It is the open source implementation of Oracle JDK version, released under GPL. When in JDK 7, Sub JDK is released on the basis of Open JDK 7 on, replace only a small amount of source code. After Sun Microsystems acquired by Oracle, Sun SDK is called Oracle JDK. Oracle JDK is based on the Oracle Binary COde License Agreement agreement. The difference is as follows:

  1. Oracle JDK will release a stable version for three years, OpenJDK published once every three months.
  2. Oracle JDK support LTS, OpenJDK only support the current version to the next version is released.
  3. Oracle JDK using Oracle Binary Code License Agreement, OpenJDK use GPL v2 protocol.
  4. Oracle JDK based on OpenJDK build, technically there is no fundamental difference.

Android and JDK

Speaking of Android and OpenJDK historical origins, or slightly more complicated. In short, Java Android initially using a protocol based Harmony Apache released later due to limitations Harmony itself and Oracle Corp. sued From Android N, Google started using OpenJDK. Then we'll talk a little expansion.

JVM and TCK

Sun's Java language was originally developed as well as developing the JVM, and the JVM specification defined. This we more clearly, as long as develop their own language based on the JVM specification, you can run on the JVM. But then in accordance with the specification development language, required by Sun's TCK (Technology Compatibility Kit) testing before it can become an officially recognized JVM language.

Harmony 和 OpenJDK

Based on the JVM specification, Apache has developed a free open-source Java implementation Harmony, and released under the Apache License v2. But the company did not give Sun Harmony TCK license.

In 2009.4.15 Sun released the OpenJDK, released under GNU GPL. At the same time it provides that only the Sun's OpenJDK derived from the use of the GPL open source implementation of OpenJDK can run TCK. After the acquisition of Sun Microsystems after Oracle took over the OpenJDK. Because of Apache's Harmony Apache protocol is incompatible with the GPL OpenJDK agreement, so Harmony TCK has not been authorized.

Android is the beginning of using Harmony as its Java class libraries, because Apache Harmony protocol used more freely. And because Harmony did not pass the TCK certification, also paving the way for the later Oracle sued Google.

Oracle and Google on JDK dispute

Oracle sued Google and later focused on two things, first, when Oracle believes Google uses Java code 37 API, the second is the development of Sun's former employees for Android project quit after nine lines of code directly copied in OpenJDK, the Android project did not authorize in accordance with the GPL, so copy the OpenJDK code that is not authorized by the GPL.

So in order to solve the problem later patent, after Android N, Android began to replace the Harmony using OpenJDK.

More than Android and JDK references:
juejin.im/entry/5abc5...
zh.wikipedia.org/zh/Android#...
gudong.name/2019/04/05/...

Some talked about the history of Java, let's look at the various Java versions of those new features. Here are just the greatest impact on the developers of some of the features -

Java 8

1. Lambda functions and interfaces

Lambda expressions do not have to believe too much introduction, finally introduced in Java 8 can greatly reduce the amount of code, the code looks more refreshing.
Interface function is one and only one abstract method , but there may be a plurality of non-abstract methods of the interface. It can be implicitly converted to Lambda expressions. We define a function interface as follows:

@FunctionalInterface
interface Operation {
    int operation(int a, int b);
}
复制代码

Redefinition for operating a Class Operation interfaces.

class Test {
    private int operate(int a, int b, Operation operation) {
        return operation.operation(a, b);
    }
}

Test test = new Test();
复制代码

Before Java 8, we want to achieve Operation Interface and passed Test.operate () method, we have to define an anonymous class that implements Operation method.

test.operate(1, 2, new Operation() {
    @Override
    public int operation(int a, int b) {
        return a + b;
    }
});
复制代码

Lambda expressions are used, so we can write:

test.operate(1, 2, (a, b) -> a + b);
复制代码

2. The reference method

By means of reference, you can use the name to point to a method. Using a pair of primers colon "::" method. Or in the example above, we add a few ways:

@FunctionalInterface
interface Operation {
    int operation(int a, int b);
}

interface Creater<T> {
    T get();
}

interface TestInt {
    int cp(Test test1, Test test2);
}

class Test {
    public static Test create(Creater<Test> creater) {
        return creater.get();
    }

    private int operate(int a, int b, Operation operation) {
        return operation.operation(a, b);
    }

    private static int add(int a, int b) {
        return a + b;
    }

    private int sub(int a, int b) {
        return a - b;
    }

    public int testM(Test test) {
        return 0;
    }

    public void test(TestInt testInt) {
        Test t1 = Test.create(Test::new); 
        Test t2 = Test.create(Test::new);
        testInt.cp(t1, t2);
    }

}
复制代码

So there are four references to the corresponding method: constructor reference
use: Class :: new

Test test = Test.create(Test::new);
复制代码

Static method reference
use: Class :: staticMethod

test.operate(1, 2, Test::add);
复制代码

Examples of methods of an object reference
use: instance :: method

test.operate(1, 2, test::sub);
复制代码

Examples of the method class reference
use: Class :: method

test.test(Test::testM);
复制代码

In fact, three methods cited above are well understood, the last reference to the class instance method, there are two conditions:

  1. First to meet instance methods, not static methods
  2. The first parameter Lambda expression of a target for calling these two examples of the method according to the above example we see, Test method takes a TestInt example, is represented (Test t1, Test t2) by Lambda expressions -> res, and we passed when calling test methods reference is Test :: testM, its argument is a Test instance, the final test.test (Test :: testM) call effect is t1.testM (t2)

3. Interface default methods and static methods

Java 8 new default interface to achieve, by default means your keyword. But it can also provide a static default method.

public interface TestInterface {
    String test();

    // 接口默认方法
    default String defaultTest() {
        return "default";
    }

    static String staticTest() {
        return "static";
    }
}
复制代码

4. Repeat comment

Java 8 support the repeated notes. Repeat annotation want to achieve before Java 8, need some way to bypass the restrictions. Such as the following code.

@interface Author {
    String name();
}

@interface Authors {
    Author[] value();
}

@Authors({@Author(name="a"), @Author(name = "b")})
class Article {
}
复制代码

In the Java 8, it can be directly used in the following manner.

@Repeatable(Authors.class)
@interface Author {
    String name();
}

@interface Authors {
    Author[] value();
}

@Author(name = "a")
@Author(name = "b")
class Article {
}
复制代码

When parsing annotations, Java 8 also provides a new API.

AnnotatedElement.getAnnotationsByType(Class<T>)
复制代码

5. Type Notes

Java 8 Before the annotation can only be used in the statement, in Java 8, annotation can be used anywhere.

@Author(name="a")
private Object name = "";
private String author = (@Author(name="a")String) name;
复制代码

6. Better type inference

Java 8 for type inference made improvements.
For example, in Java 7 in the following wording:

List<String> stringList = new ArrayList<>();
stringList.add("A");
stringList.addAll(Arrays.<String>asList());
复制代码

In writing the Java 8 in improved type inference can be done automatically.

List<String> stringList = new ArrayList<>();
stringList.add("A");
stringList.addAll(Arrays.asList());
复制代码

7. Optional

Java 8 in a new class to solve Optional null pointer exception. Optional is a container object can be saved to null. By isPresent () method whether the detected value is present, the object returned by the get () method.
In addition, Optional also provides many other useful methods, you can view the specific document . The following are some sample code.

// 创建一个 String 类型的容器
Optional<String> str = Optional.of("str");
// 值是否存在
boolean pre = str.isPresent();
// 值如果存在就调用 println 方法,这里传入的是 println 的方法引用
str.ifPresent(System.out::println);
// 获取值
String res = str.get();
// 传入空值
str = Optional.ofNullable(null);
// 如果值存在,返回值,否则返回传入的参数
res = str.orElse("aa");
str = Optional.of("str");
// 如果有值,对其调用映射函数得到返回值,对返回值进行 Optional 包装并返回
res = str.map(s -> "aa" + s).get();
// 返回一个带有映射函数的 Optional 对象
res = str.flatMap(s -> Optional.of(s + "bb")).flatMap(s -> Optional.of(s + "cc")).get();
复制代码

8. Stream

Java 8 in the new Stream provides a new class of data processing. In this way the elements in the set considered as a streaming transmission in the pipeline, through a series of processing nodes, the final output.
Stream provided on a specific method, reference can be the API . The following are some sample code.

List<String> list = Arrays.asList("maa", "a", "ab", "c");
list.stream()
        .filter(s -> s.contains("a"))
        .map(s -> s + "aa")
        .sorted()
        .forEach(System.out::println);

System.out.println("####");
list.parallelStream().forEach(System.out::println);

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
int res = numbers.stream().map(i -> i + 1).mapToInt(i -> i).summaryStatistics().getMax();
System.out.println(res);
复制代码

9. Date Time API

Java 8 in the new date and time API is used to strengthen the handling of date and time, including LocalDate, LocalTime, LocalDateTime, ZonedDateTime and so on, the API can refer to the official documents as well as this blog , written in great detail. The following sample code.

LocalDate now = LocalDate.now();
System.out.println(now);
System.out.println(now.getYear());
System.out.println(now.getMonth());
System.out.println(now.getDayOfMonth());

LocalTime localTime = LocalTime.now();
System.out.println(localTime);
LocalDateTime localDateTime = now.atTime(localTime);
System.out.println(localDateTime);
复制代码

10. Base64 support

Java 8 standard library provides support for the Base 64 encoded. Specific can see API reference documentation . The following sample code.

String base64 = Base64.getEncoder().encodeToString("aaa".getBytes());
System.out.println(base64);
byte[] bytes = Base64.getDecoder().decode(base64);
System.out.println(new String(bytes));
复制代码

11. A parallel array ParallelSort

Java 8 provides an array of parallel operations, including parallelSort the like, and specific reference to the API .

Arrays.parallelSort(new int[] {1, 2, 3, 4, 5});
复制代码

12. Other new features

  • Java.util.concurrent.atomic concurrent enhancement in the package also adds the following classes: DoubleAccumulator DoubleAdder LongAccumulator LongAdder
  • It provides a new Nashorn javascript engine
  • Command-line tool provides jjs, is a given Nashorn can be used to execute JavaScript source code
  • Provides a new class of analytical tools rely jdeps
  • New features of the JVM JVM permanent memory region has been replaced metaspace (JEP 122). JVM parameters -XX: PermSize and -XX: MaxPermSize is XX: MetaSpaceSize and -XX: MaxMetaspaceSize instead.

It can be seen overall improvements in Java 8 is great, the most important is the introduction of Lambda expressions, simplify the code.

Other improvements may refer www.oracle.com/technetwork...

Java 9

1. Jigsaw module system

In Java 9 before packing and dependence are based on the JAR package. JRE included rt.jar, nearly 63M, that is to run a simple Hello World, also we need to rely on such a large jar package. In the modular system in Java 9 raised, this point has been improved. About modular system specifically look at this article .

2. JShell REPL

Java 9 provides an interactive interpreter. With JShell later, Java can finally be like Python, Node.js as running some code in the Shell and direct the outcome of.

3. The private interface method, the method using the private interface

Java 9 private methods can be defined in the interface. Sample code is as follows:

public interface TestInterface {
    String test();

    // 接口默认方法
    default String defaultTest() {
        pmethod();
        return "default";
    }

    private String pmethod() {
        System.out.println("private method in interface");
        return "private";
    }
}
复制代码

4. The method set immutable instance factory

In the past, we want to create an immutable collection, you need to create a variable collection, and then use unmodifiableSet create immutable collections. code show as below:

Set<String> set = new HashSet<>();
set.add("A");
set.add("B");
set.add("C");

set = Collections.unmodifiableSet(set);
System.out.println(set);
复制代码

Java 9 provides new API to create immutable collections.

List<String> list = List.of("A", "B", "C");
Set<String> set = Set.of("A", "B", "C");
Map<String, String> map = Map.of("KA", "VA", "KB", "VB");
复制代码

5. Improved try-with-resources

Java 9 does not require additional definition of a variable in the try. Java before 9 need to use try-with-resources:

InputStream inputStream = new StringBufferInputStream("a");
try (InputStream in = inputStream) {
    in.read();
} catch (IOException e) {
    e.printStackTrace();
}
复制代码

InputStream variables can be used directly in Java 9, no additional re-define new variables.

InputStream inputStream = new StringBufferInputStream("a");
try (inputStream) {
    inputStream.read();
} catch (IOException e) {
    e.printStackTrace();
}
复制代码

6. Multi-compatible version of the jar package

Java 9 in support of maintaining different versions of Java classes and resources in the same JAR.

7. enhance Stream, Optional, Process API

8. Added HTTP2 Client

9. Enhanced Javadoc, increasing the output HTML 5 documents, and increase the search function

10. Enhanced @Deprecated

We deprecated since been added to the properties and forRemoval

11. The operator enhanced diamond "<>", can be used in anonymous inner classes.

Before Java 9, inside the anonymous class you need to specify the generic type, as follows:

Handler<? extends Number> intHandler1 = new Handler<Number>(2) {
}
复制代码

In Java 9, the type inference can be done automatically, as follows:

Handler<? extends Number> intHandler1 = new Handler<>(2) {
}
复制代码

12. Multi-resolution image API: multi-resolution image is defined API, developers can easily operate and display the images with different resolutions.

13. Improved CompletableFuture API

Asynchronous mechanism CompletableFuture class can take action when ProcessHandle.onExit method exits.

Other improvements may refer docs.oracle.com/javase/9/wh...

Java 10

1. New type inference local var

var a = "aa";
System.out.println(a);
复制代码

var keyword can only be used for local variables and loop variable declaration.

2. Removal Tool javah

Javah remove the tool from the JDK, use javac -h instead.

3. The unified garbage collection interfaces, improved GC and other housekeeping

Other features

  • ThreadLocal handshake interactive
    new method to perform the callback on a thread of JDK 10 is introduced, it is convenient to stop a single thread instead of stopping all or a thread are kept.

  • Java-based experimental JIT compiler
    Java 10 opens the Java JIT compiler Graal, as a Linux / x64 experimental JIT compiler on the platform.

  • Provide default CA root certificate

  • The JDK ecology into a single warehouse
    this JEP main goal is to perform some memory management, and combined into a repository of many JDK repository ecology.

Some other improvements can refer www.oracle.com/technetwork...

Java 11

1. Lambda use var

(var x, var y) -> x.process(y)
复制代码

2. String API Enhancements

Java 11 new series of string processing methods, such as:

// 判断字符串是否为空白
" ".isBlank(); 
" Javastack ".stripTrailing();  // " Javastack"
" Javastack ".stripLeading();   // "Javastack "
复制代码

3. Standardization HttpClient API

4. java compile and run directly, eliminating the first step run javac class compiled

5. Added support for TLS 1.3 in

Some other improvements can refer www.oracle.com/technetwork...

Java 12

switch expression

Java after 12, switch not only as a statement or as an expression.

private String switchTest(int i) {
    return switch (i) {
        case 1 -> "1";
        default -> "0";
    };
}
复制代码

Some other improvements can refer www.oracle.com/technetwork...

to sum up

summary1

Reference material

Java8
ifeve.com/java-8-feat…
juejin.im/post/5ae6bf…
wizardforcel.gitbooks.io/java8-tutor…
www.oracle.com/technetwork…
www.oracle.com/technetwork…

Java9
www.runoob.com/java/java9-…
docs.oracle.com/javase/9/wh…
www.twle.cn/c/yufei/jav…

Java10
baijiahao.baidu.com/s?id=159443…
blog.csdn.net/visant/arti…
www.oracle.com/technetwork…

Java 11
openjdk.java.net/projects/jd…
www.ctolib.com/topics-1351…

Java 12
zhuanlan.zhihu.com/p/59798800

Other
www.baeldung.com/oracle-jdk-...
zh.wikipedia.org/wiki/Java version ...
www.zhihu.com/question/19...
juejin.im/post/5ca1c7...
gudong.name/2019/04/05 / ...

Welcome attention to the following account, get the latest technical articles:
Github
Nuggets
know almost

Guess you like

Origin juejin.im/post/5d5950806fb9a06b0a277412