JDK10 features

JAVA10 overview

On March 21, 2018, Oracle officially announced the official release of JAVA10.

Neither JAVA9 nor JAVA10 is an LTS (Long-Term-Support) version. Unlike previous major JAVA version upgrades, these two only have about half a year of development and maintenance time.

JAVA10 defines a total of 109 new features, including JEP, which is the only real new feature for programmers. There are also some new APIs and JVM specifications as well as changes in the JAVA language specifications.

For the 12 JEPs (JDK Enhancement Proposal) of JAVA10, please refer to the official document http://openjdk.java.net/projects/jdk/10/

image.png

Specific new features:

286: Local variable type inference

296: JDK library merge

304: Unified garbage collection interface

307: Provide parallel Full GC for G1

310: Application class data sharing

312: ThreadLocal handshake interaction

313: Remove javah tool included in JDK

314: Use additional Unicode language tag extensions

316: Ability to allocate memory occupancy to user-specified spare memory devices

317:Using JAVA-based JIT compiler

319: Root certificate

322: Time-based releases

changes in grammatical level

Type inference for local variables

Explicit type declarations of local variables are often considered unnecessary. Giving a nice name can often clearly express how to proceed, reducing verbosity and formal code, avoiding redundant information, and aligning variables. First name, easy to read.

As JAVA programmers, when declaring a variable, we usually write the variable type twice, the first time is used to declare the variable type, and the second time is used for the constructor

List<String> list =new ArrayList<>();

The declaration type of variables is complex and long to write, especially with the use of generics

Iterator<Map.Entry<Integer,Student>> iterator=set.iterator();

We also often declare a variable that is only used once, on the next line of code:

URL url=new URL("http://www.boge.com");
URLConnection connection= url.openConnection();
Reader reader =new BufferedReader(new InputStreamReader(connection.getInputStream));

In short: the variable identifier (variable name) can let me clearly know how to use the variable, there is no need to add a long list of type declarations in front of it

Local variable inference is somewhat similar to the way of writing weak variable types in JavaScript. The previous data type is inferred from the subsequent data, and the declaration of the data type is unified as var.

image.png

Scenarios where type inference cannot be used

There are situations where we cannot use type inference. Specifically, there are the following scenarios

variable declaration

The declaration of a variable cannot use type inference because it cannot be inferred

// 根据右边的数据推断类型,不赋值压根没给推断的机会,这是错的
var userName;

Insert image description here

Initial value null

The case where the initial value is null is also not acceptable. Because there is also no way to infer

// null值无法推断数据类型,这是不能使用类型推断
var userName=null;

Insert image description here

lambda expression

        // 这个是可以的
        Supplier<Double> supplier=()-> Math.random();
        // 这个是不行的 lambda表达式需要显式数据类型,不然无法明确是哪个接口
        var supplier2=()->Math.random();

method reference

        // 这是可以的进行方法引用的
        Consumer<String> consumer=System.out::println;
        // 这是不可以的,无法明确接口类型
        var consumer2=System.out::println;

Insert image description here

Static initialization of arrays

        //动态初始化数据可以进行类型推断
        var arr =new String[10];
        //静态初始化数组不可以使用类型推断
        var arr2 = {
    
    "a12","222","333","444","555"};

Member variables cannot be used

Type inference is only for local variables, member variables cannot use type inference

public class Demo07 {
    
    
    // 成员变量不能使用类型推断
    var name="小明";
    public void testMethod1(){
    
    
        // 局部变量可以使用类型推断
        var localname="小明";
    }
}

Other impossible scenarios

// 情况1 没有初始化的局部变量声明
var s; var x=null;
// 情况2 方法的返回值类型
public var test1()
// 情况3 方法的参数类型
public void test2(var a,var b)
// 情况4 构造器的参数类型
public Person(var name,var age)
// 情况5 属性
class Person{
    
    
    var name;
}
// 情况6 catch块
try{
    
    
}catch(var e){
    
    
}

The following two points need to be noted

  1. var is not a keyword

We don't need to worry about variable names or method names conflicting with var, because var is not actually a keyword, but a type name, and it is only used when the compiler needs to know the type. In addition, It is just an ordinary legal identifier. In other words, except that it cannot be used as a class name, everything else can be used. But which fool insists on using var as a class name?

  1. This is not JavaScript after all

Var does not change the fact that Java is a static language. The compiler is responsible for inferring the type and writing the result into the bytecode. In other words, the data type is still in the bytecode, and Java is still strongly typed. The programming language is not written explicitly by the developers. JavaScript is a weakly typed interpreted scripting language, and the type inference here is two different things.

API level changes

copyOf method of collection

A new copyOf method is added to collections in JDK10. Used to create read-only collections

        // JAVA9中新增创建只读的方法
        var strings1 = List.of("Python", "JAVA", "Golang");
        // JAVA10中新增的创建只读集合的方法
        var strings2 = List.copyOf(strings1);
        // 判断两个集合在内存上是否是同一个,结果为true
        System.out.println(strings1==strings2);

        // 创建一个普通集合
        var strings3=new ArrayList<String>();
        // 通过copyOf方法创建一个只读集合
        var strings4 = List.copyOf(strings3);
        // 判断两个集合在内存上是否是同一个,结果为false
        System.out.println(strings3==strings4);

Conclusion : The copyOf method returns a read-only collection through a collection. If the parameter is originally a read-only collection, then the parameter is returned. If the parameter is not a read-only collection, create another read-only collection and return it.

Summarize

JAVA10 does not have many features and changes. After all, the development time is only half a year.

Guess you like

Origin blog.csdn.net/qq_28314431/article/details/132917043