“This is my third day of the November Gwen Challenge.The final text challenge in 2021”

1.JDK

Release cycle of the JDK

Its and OracleJDK

The differences are as follows:

  • Stable versions of the OracleJDK are released every 3 years, and OpenJDK every 3 months
  • OracleJDK supports LTS (long-trem-support), OpenJDK only supports scheduled releases to the next release
  • The Oracle Binary Code License is used for the OracleJDK, and the GPL V2 is used for the OpenJDK
  • OracleJDK is built on OpenJDK

Android and the JDK

Android started with the Java version of Harmony based on the Apache protocol, and started with the OpenJDK from Android N

JAVA8

Lambda and functional interfaces

A functional interface is one that has one and only abstract method, but can have multiple non-abstract methods, and can be implicitly converted to a Lambda expression

Define a functional interface

@FunctionalInterface
public interface IOperation {
    int operation(int a, int b);
}
Copy the code

Define action classes

public class OperationImpl {
    public int operation(int a, int b, IOperation operation) {
        return operation.operation(a, b);
    }
}

OperationImpl oImpl = new OperationImpl();
Copy the code

Prior to java8 operations needed to define anonymous implementations,

oImpl.operation(1, 2, new IOperation(){
    public int operation(int a, int b) {
        return a + b;
    };
});
Copy the code

After using a Lambda expression

oImpl.operation(2, 3, (a, b) -> a + b);
Copy the code

2. Method references

Method application refers to a method with the name of the method and applies the method with a pair of colons.

@FunctionalInterface public interface IOperation { int operation(int a, int b); } interface Creator<T> { T get(); } interface TestInt { int cp(Test test, Test test2); } public class Test { public static Test create(Creator<Test> creator) { return creator.get(); } public int operation(int a, int b, IOperation 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); }}Copy the code

Corresponding reference method

Constructor reference, Class::new

Test test = Test.Create(Test::new);

StaticMethod reference, Class::staticMethod

test.operation(1, 2, Test::add);

Object instance::method

test.operation(1, 2, test::sub);

Class::method instance reference

test.test(Test::testM);

3. Interface default method and static method

The interface provides the default method by using the default method

public interface KingListener { void getName(); // Default void getAge() {system.out.println ("Age is 1024."); }}Copy the code

4. Repeat the notes

Prior to Java8, repeated annotations need to look like this:

@interface Author {
    String name();
}

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

@Authors({@Author(name="a"), @Author(name = "b")})
class Article {
}
Copy the code

After Java8, the following options are available:

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

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

@Author(name = "a")
@Author(name = "b")
class Article {
}
Copy the code

When parsing annotations, Java8 provides a new Api

AnnotatedElement.getAnnotationsByType(Class<T>)
Copy the code

5. Type annotations

Annotations before Java8 can be anywhere after Java8 in the declaration

@Author(name = "a")
private Object name = "";

private String author = (@Author(name="a")String) name;
Copy the code

6. Better type inference

Java8 has updated type inference

Java7 is written as follows

List<String> stringList = new ArrayList<>();
stringList.add("A");
stringList.addAll(Arrays.<String>asList());
Copy the code

After java8

List<String> stringList = new ArrayList<>();
stringList.add("A");
stringList.addAll(Arrays.asList());
Copy the code

7.Optional

Java8 added the Optional class to handle null pointer exceptions. Optional is a container object that can hold null. The isPresent() method is used to check whether the value exists, and the get() method is used to get the returned object

// Create a container of type String Optional<String> STR = Optional. Of (" STR "); Boolean pre = str.ispresent (); // If the value exists, the println method is called. In this case, the println method is passed in. String res = str.get(); // pass in null STR = optional. ofNullable(null); // Return the value if it exists, otherwise return the passed argument res = str.orelse ("aa"); str = Optional.of("str"); // If there is a value, call the mapping function to get the return value, wrap the return value Optional and return res = str.map(s -> "aa" + s).get(); Res = str.flatMap(s -> optional. of(s + "bb")).flatMap(s -> optional. of(s + "cc")).get();Copy the code

8.Stream

The Stream class has been added to Java8 to provide a new way of processing data. You can treat a collection of elements as a Stream, passing through a series of processing nodes, and finally output the result

List<String> list = Arrays.asList("maa", "a", "abbb", "cccc");
list.stream().filter(s -> s.contains("a"))
    .map(s -> s + "sb")
    .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);
int aaaaaa = numbers.stream().map(i -> i + 1).mapToInt(i -> i).summaryStatistics().getMax();
System.out.println(aaaaaa);
Copy the code

9. Date and time Api

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);
Copy the code

10. Base64 support

Java8 provides support for Base64 encoding

byte[] base64 = Base64.decode(content, Base64.NO_WRAP);
Base64.encodeToString(data, Base64.NO_WRAP);
Copy the code

Parallel array ParallelSort

Arrays.parallelSort(new int[] {1, 2, 3, 4, 5});
Copy the code

12. Other new features

  • Enhancements to concurrency
  • Java. Util. Concurrent. Atomic package has increased

DoubleAccumulator

DoubleAdder

LongAccumulator

LongAdder

  • The new Nashorn javascript engine is available
  • JJS is a command line tool for Nashorn that can be used to execute JavaScript source code
  • A new class dependency analysis tool, JDEPS, is provided
  • New JVM feature, permanent memory area replaced by Metaspace (JEP 122)

\

Java9

1.Jigsaw module system

Prior to Java 9, packaging and dependencies were based on JAR packages. The JRE contains rt.jar, which is nearly 63 meters, which means you would need to rely on a jar package to run a simple Hello World. The modular system, introduced in Java 9, improves on this.

2.JShell REPL

Java 9 provides an interactive interpreter. With JShell, Java can finally run some code in the Shell like Python and Node.js and get results directly.

3. Private interface methods, which are used in interfaces

public interface TestInterface { String test(); // Default String defaultTest() {pmethod(); return "default"; } private String pmethod() { System.out.println("private method in interface"); return "private"; }}Copy the code

4. Assemble immutable instance factory methods

Previously, we wanted to create an immutable collection by first creating a mutable collection and then using unmodifiableSet to create an immutable collection. The code is as follows:

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

set = Collections.unmodifiableSet(set);
System.out.println(set);
Copy the code

Java 9 provides a new API for creating 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");
Copy the code

5. Improve the try – with – resources

There is no need in Java 9 to define an additional variable in the try. Before Java 9 you needed to use try-with-resources like this:

InputStream inputStream = new StringBufferInputStream("a");
try (InputStream in = inputStream) {
    in.read();
} catch (IOException e) {
    e.printStackTrace();
}
Copy the code

In Java 9, you can use the inputStream variable directly without having to define new variables.

InputStream inputStream = new StringBufferInputStream("a");
try (inputStream) {
    inputStream.read();
} catch (IOException e) {
    e.printStackTrace();
}
Copy the code

6. Jar packages compatible with multiple versions

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

7. Enhanced Stream, Optional, Process API

8. Add HTTP2 Client

9. Enhanced Javadoc to increase the output of HTML 5 documents and add search capabilities

10. @ Deprecated

New since and forRemoval attributes for Deprecated

11. Improved the diamond operator “<>” to be used in anonymous inner classes.

Prior to Java 9, internal anonymous classes needed to specify generic types as follows:

Handler<? extends Number> intHandler1 = new Handler<Number>(2) {
}
Copy the code

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

Handler<? extends Number> intHandler1 = new Handler<>(2) {
}
Copy the code

12. Multi-resolution Graphics API: Define the multi-resolution graphics API so that developers can easily manipulate and display images in different resolutions.

13. Improved CompletableFuture API

The asynchronous mechanism of the CompletableFuture class can perform an operation when the processhandle. onExit method exits.

Java 10

1. Added local type inference var

var a = "aa";
System.out.println(a);
Copy the code

Note: The var keyword is currently only used for local variables and for loop variable declarations.

2. Delete the javah tool

Remove the Javah tool from the JDK and use javac -h instead.

3. Unified garbage collection interface, improved GC and other housekeeping management

Other features

ThreadLocal handshake interaction

JDK 10 introduces a new method of performing callbacks on threads, which conveniently stops a single thread rather than all or none at all.

An experimental Java-based JIT compiler

Java 10 turns on the Java JIT compiler Graal, which is used as an experimental JIT compiler for Linux/X64 platforms.

Provides the default CA root certificate

Integrate the JDK ecosystem into a single repository

The main goal of this JEP is to perform some memory management and combine the many repositories in the JDK ecosystem into one repository.

ava 11

1. Use var in Lambda

(var x, var y) -> x.process(y)
Copy the code

2. String API enhancements

Java 11 has a new set of string handling methods, such as:

// check if the string isBlank "".isblank (); " Javastack ".stripTrailing(); // " Javastack" " Javastack ".stripLeading(); // "Javastack "Copy the code

3. Standardize the HttpClient API

3. Java is directly compiled and run, eliminating the need to compile javAC to generate class and then run it

3. Added support for TLS 1.3

Java12

switch

After Java 12, switches can be used not only as statements, but also as expressions.

private String switchTest(int i) {
    return switch (i) {
        case 1 -> "1";
        default -> "0";
    };
}
Copy the code

conclusion