Generics in Java

Foreword

Generics (Generics), generalization is understood that the type, i.e., parameterized types from the literal meaning.
We all know that generics are a very important new features JDK5 provided it has a lot of excellent quality: the ability to put a lot of questions in advance from the compiler to run, making programs more robust.

But because Java5 to maintain good backward compatibility, so the occasion of the launch until now, it is a false thing: there is only at compile time, compiled into a .class file does not exist, this is the so-called generic erased.

The following comparison of the two classes, a class is a general, is a generic class:

class Generics {
    Object k;
    Object v;

    public Generics(Object k, Object v) {
        this.k = k;
        this.v = v;
    }
}

class Generics<K, V> {
    K k;
    V v;
    public Generics(K k, V v) {
        this.k = k;
        this.v = v;
    }
}

After declaring a general generic class on the class name, there may be a plurality of generic parameters, formed by angle brackets type parameter list.

Generic Interface
public interface Generator<T> {
    public T next();
}

This generic interface design, is declared a factory design pattern is used to generate interface. Such as our common iterator interface Iterableis such an interface

public interface Iterable<T> {
    Iterator<T> iterator();
}
Generic method

Examples divided into static generic methods and generic methods.

    public <T> T genericMethod (T T) {
         return T; 
    } 

    // Note that the static method <T> must be behind the static ~ 
    public  static <T> T genericStaticMethod (T T) {
         return T; 
    }

It should be a little note:

public  class the Main <T> { 

    // static methods can not directly use the generic class parameter T needs to own declarations
     // shaped so as to write the correct way: public static <T> T genericStaticMethod (T t) {...} 
    public  static T genericStaticMethod (T T) {
         return T; 
    } 

    // example method may be used as generic parameter class declaration 
    public T genericMethod (T T) {
         return T; 
    } 
}

Static method using generic parameters required its own separate statement, otherwise the compilation error.

Generic class declaration statement and a generic method is slightly different, which is listed in parameter list type (may be a plurality of generic type) with angle brackets before the return type, and the function passed parameter type may be utilized generics are represented.

Generic erased

public class Main {
    public static void main(String[] args) {
        List<String> c1 = new ArrayList<>();
        List<Integer> c2 = new ArrayList<>();
        Class<? extends List> class1 = c1.getClass();
        Class<? extends List> class2 = c2.getClass();
        
        System.out.println(class1 == class2);
    }
}

The output is: true
fact, there may be many small partners can not understand, obviously the generic type to pass it is not the same, why is it still the same Class? ? ? Here we must say Java language unique to the generic - generic erased.

This example illustrates: When we declare List <String> and a List <Integer>, at run-time is actually the same, they are List, and specific types Integer and String parameter information is erased.

Generic wildcards

<? super T>Kazu <? extends T>Kazu<?>




Guess you like

Origin www.cnblogs.com/deityjian/p/11370166.html