Initialization of Java 7 Diamond operators and derived classes

class A {}
class A {}

class B extends A {}

class Holder<T> {

    T object;

    Holder(T object) {
        this.object = object;
    }
}

There is a holder class to hold some objects created using generics In main(), when the diamond operator is used for initialization, it will not compile (Java 7), and the derived class is passed to the constructor of holder (A / B is required):

public static void main(String[] args) {    
    Holder<A> holder = new Holder<>(new B());        
}

However, if the base type is specified in the right section, it will compile and run:

public static void main(String[] args) {
    Holder<A> holder = new Holder<A>(new B());
}

Why? Does the diamond operator use the same type parameter as the left to define the right part of the assignment?

Solution

First observe:

Holder<B> h = new Holder<>(new B());

Compile with Java 8 and Java 7, and both create holder < b > in that case So it's good to use < > constructor with parameters

However:

Holder<A> h = new Holder<>(new B());

>Java 7 first evaluates the right side and determines that it is holder < b > And give a compilation error because a holder < b > can't be converted into a holder < a >. > Java 8 goes one step further, inferring that the new holder < < a > (New B ()) can actually work and complete it magically automatically This is due to a new feature called "target typing" – see the last part of the tutorial on type information for an overview

In more detail, the improvement of Java 8 is due to the introduction of poly expressions (emphasizing my):

This is a very powerful feature of Java 8 (Java 7 only provides independent expressions regardless of expression context)

Generic class instance creation is a polysemy expression. JLS #15.9 explains (emphasize my):

Due to the new rules, Java 8 allows you to use the second form above and automatically infer that the new B () should be treated as a (widening reference transformation), and you intend to create a holder < a > under these circumstances.

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
分享
二维码
< <上一篇
下一篇>>