Home > Java > javaTutorial > body text

11.Java Basics - Generics

黄舟
Release: 2017-02-27 10:43:58
Original
944 people have browsed it

Basic concept

The essence of generics is the application of Parameterized Type, That is to say, the data type being operated is specified as a parameter, and the specific type is specified when used.

This parameter type can be used in the creation of classes, interfaces and methods, called generic classes, generic interfaces and generic methods respectively.


1. Development

Before JDK 1.5, only Object is the parent class of all types and type cast The combination of these characteristics can achieve type generalization.

Therefore, during compilation, the compiler cannot check whether the cast of this Object is successful, which may cause a ClassCastException (cast exception).

Let’s take a look at an example to understand the role of generics:

  • Without using generics (before 1.5)

ArrayList arrayList = new ArrayList();
arrayList.add(100);
arrayList.add("abc");//因为不知道取出来的值的类型,类型转换的时候容易出错  String str = (String) arrayList.get(0);
Copy after login
  • Using generics (after 1.5)

ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("abc");//因为限定了类型,所以不能添加整形,编译器会提示出错arrayList.add(100);
Copy after login

2. Terminology

// 以 ArrayList<E>,ArrayList<Integer> 为例:ArrayList<E>:泛型类型

E:类型变量(或者类型参数)

ArrayList<Integer> :参数化的类型

Integer:类型参数的实例(或实际类型参数)

ArrayList :原始类型
Copy after login

3. Explore

Generic class

class Demo<T> {    private T value;

    Demo(T value) {        this.value = value;
    }    public T getValue() {        return value;
    }    public void setValue(T value) {        this.value = value;
    }
}public class Test {
    public static void main(String[] args) {
        Demo<String> demo = new Demo("abc");
        demo.setValue("cba");
        System.out.println(demo.getValue()); // cba
    }
}
Copy after login

Generic interface

interface Demo<K, V> {    void print(K k, V v);
}

class DemoImpl implements Demo<String, Integer> {    @Override
    public void print(String k, Integer v) {
        System.out.println(k + "-" + v);
    }
}public class Test {
    public static void main(String[] args) {
        Demo<String, Integer> demo = new DemoImpl();
        demo.print("abc", 100);
    }
}
Copy after login

Generic method

public class Test {
    public static void main(String[] args) {        int num = get("abc", 100);
        System.out.println(num);
    }    // 关键 --> 多了 <K, V> ,可以理解为声明此方法为泛型方法
    public static <K, V> V get(K k, V v) {        if (k != null) {            return v;
        }        return null;
    }
}
Copy after login

Type qualification

Type qualification can be used in generic classes, generic interfaces and generic methods, but please pay attention to the following points:

  • Regardless of whether the qualification is a class or an interface, the keyword extends is used uniformly

  • You can use the & symbol to give multiple limitations

  • If there are both interfaces and classes, then there must be only one class and it must be placed first. For example:

public static <T extends Comparable&Serializable> T get(T t1,T t2)
Copy after login

Let’s analyze the role of type qualification...


1. Do not set boundaries for type parameters

Observe the following code, a compilation error will occur when the type parameters are not type-qualified. The reason is as follows:

  • Because before compilation, the compiler cannot confirm what type the generic type (T) is

  • So it defaults to T Is a primitive type (Object).

  • So you can only call the Object method, but not the compareTo method.

public static <T> T get(T t1,T t2) {    //编译错误
    if(t1.compareTo(t2)>=0);    return t1;
}
Copy after login

2. Set bounds on type parameters

When setting bounds on type parameter T, the compilation error will not occur Happens again. Because at this time the compiler defaults to the original type of T as Comparable.

public static <T extends Comparable> T get(T t1,T t2) {    if(t1.compareTo(t2)>=0);    return t1;
}
Copy after login

Type erasure

  • Generics in Java are basically implemented at the compiler level.

  • The generated Java bytecode does not contain type information in generics.

  • The type parameters added when using generics will be removed by the compiler during compilation. This process is called type erasure.


Look at the following example:

public class Test {
    public static void main(String[] args) {
        ArrayList<String> arrayList1 =new ArrayList<String>();
        ArrayList<Integer> arrayList2 = new ArrayList<Integer>();        // true
        System.out.println(arrayList1.getClass() == arrayList2.getClass());
    }
}
Copy after login

Observe the code, two ArrayList arrays are defined here:

  • One is the ArrayList generic type, which can only store strings, and the other is the ArrayList generic type, which can only store integers.

  • By comparing their class objects, the result is found to be true.

  • Explanation that the generic types String and Integer are erased during the compilation process, leaving only the original type (i.e. Object).


Let’s look at another example:

public class Test {
    public static void main(String[] args) throws Exception{
        ArrayList<String> arrayList =new ArrayList<String>();
        arrayList.add("abc");
        arrayList.getClass().getMethod("add", Object.class).invoke(arrayList, 100);         for (int i=0;i<arrayList.size();i++) {  
                System.out.println(arrayList.get(i));  
            }  
    }
}
Copy after login

Observe the code, here an ArrayList generic type is instantiated as an Integer object

  • If you call the add method directly, only integer data can be stored.

  • Use reflection to call the add method, but you can store strings.

  • Explanation Integer generic instances are erased after compilation, leaving only the original type.


1. Raw type

  • The raw type (raw type) erases the generic information , and finally the real type of the type variable in the bytecode.

  • Any generic type parameter has a corresponding primitive variable.

  • Once a type variable is erased (crased), it will be replaced with its qualified type (unqualified variables are Object).

// 此时 T 是一个无限定类型,所以原始类型就是 Objectclass Pair<T> { } 

// 类型变量有限定,原始类型就用第一个边界的类型变量来替换,即Comparableclass Pair<T extends Comparable& Serializable> { }  

// 此时原始类型为 Serializable,编译器在必要的时要向 Comparable 插入强制类型转换
// 为了提高效率,应该将标签(tagging)接口(即没有方法的接口)放在边界限定列表的末尾class Pair<T extends Serializable&Comparable>
Copy after login

2. Type of type parameter

In the following example, the type parameter refers to T, and the type of T is the so-called The type of [type parameter].

Observing the code, we can draw the following conclusions:

  • Do not specify the type of [type parameter T]. When the types of parameters are inconsistent, the original type takes the same parent class. The minimum level

  • When specifying the type of [type parameter T], the original type can only be the specified type or a subclass of the type

public class Test {    // 定义泛型方法
    public static <T> T add(T x, T y) {        return y;
    }    public static void main(String[] args) {        // 1.不指定泛型

        // 两个参数都是 Integer,所以 T 为 Integer 类型
        int i = Test.add(1, 2); 

        // 两个参数分别是 Integer,Float,取同一父类的最小级,T 为 Number 类型
        Number f = Test.add(1, 1.2);        // T 为 Object
        Object o = Test.add(1, "asd");        // 2.指定泛型

        // 指定了Integer,所以只能为 Integer 类型或者其子类
        int a = Test.<Integer> add(1, 2);        //编译错误,指定了 Integer,不能为Float
        int b=Test.<Integer>add(1, 2.2); 

         // 指定为Number,所以可以为 Integer,Float
        Number c = Test.<Number> add(1, 2.2);
    }

}
Copy after login

3. Type checking

The type checking of generics is for the reference, not for the referenced object itself.

In the following example, list is a reference object, so the type check is against it.

// 没有进行类型检查,等价于 ArrayList list = new ArrayLis()ArrayList list = new ArrayList<String>();
list.add(100);
list.add("hello");// 进行编译检查,等价于 ArrayList<String> list = new ArrayList<String>();ArrayList<String> list = new ArrayList();
list.add("hello");
list.add(100);  // 编译错误
Copy after login

4.类型擦除与多态的冲突

来看下面的例子,这里定义了一个泛型类 Parent,一个实现它的子类 Son,并在子类中重写了父类的方法。

class Parent<T> {    private T value;    public T getValue() {        return value;
    }    public void setValue(T value) {        this.value = value;
    }
}

class Son extends Parent<String>{    @Override
    public void setValue(String value) {         super.setValue(value);
    }    @Override
    public String getValue(){        return super.getValue();}
    }
Copy after login

在上面提到过泛型的类型参数在编译时会被类型擦除,因此编译后的 Parent 类如下:

class Parent {    private Object value;    public Object getValue() {        return value;
    }    public void setValue(Object value) {        this.value = value;
    }
}
Copy after login

此时对比 Parent 与 Son 的 getValue/setValue 方法,发现方法的参数类型已经改变,从 Object -> String,这也意味着不是重写(overrride) 而是重载(overload)。

然而调用 Son 的 setValue 方法, 发现添加 Object 对象时编译错误。说明也不是重载。

public class Test {
    public static void main(String[] args) {
        Son son = new Son();
        son.setValue("hello");        // 关键 -->编译错误
        son.setValue(new Object());
    }
}
Copy after login

那么问题来了,通过上面的分析?Son 中定义的方法到底是重写还是重载?答案是:重写。这里 JVM 采用了桥方法(Brige)来解决类型擦除和多态引起的冲突。

我们对 Son 进行反编译(”Javap -c 类名.class”),得到如下内容:

Compiled from "Test.java"class Son extends Parent<java.lang.String> {
  Son();
    Code:       0: aload_0       
       1: invokespecial #8                  // Method Parent."<init>":()V
       4: return        

  public void setValue(java.lang.String);
    Code:       0: aload_0       
       1: aload_1       
       2: invokespecial #16                 // Method Parent.setValue:(Ljava/lang/Object;)V
       5: return        

  public java.lang.String getValue();
    Code:       0: aload_0       
       1: invokespecial #23                 // Method Parent.getValue:()Ljava/lang/Object;
       4: checkcast     #26                 // class java/lang/String
       7: areturn       

  public java.lang.Object getValue();
    Code:       0: aload_0       
       1: invokevirtual #28                 // Method getValue:()Ljava/lang/String;
       4: areturn       

  public void setValue(java.lang.Object);
    Code:       0: aload_0       
       1: aload_1       
       2: checkcast     #26                 // class java/lang/String
       5: invokevirtual #30                 // Method setValue:(Ljava/lang/String;)V
       8: return        }
Copy after login

发现这里共有 4 个 setValue/getValue 方法,除了 Son 表面上重写的 String 类型,编译器又自己生成了 Object 类型的方法,也称为桥方法。结果就是,编译器通过桥方法真正实现了重写,只是在访问时又去调用表面的定义的方法。


注意事项

  • 不能用基本类型实例化类型参数,可以用对应的包装类来实例化类型参数

// 编译错误ArrayList<int> list = new ArrayList<int>();// 正确写法ArrayList<Integer> list = new ArrayList<Integer>();
Copy after login
  • 参数化类型的数组不合法

Demo<T >{
}public class Test {
    public static void main(String[] args) {        // 编译错误 --> 类型擦除导致数组变成 Object [],因此没有意义
        Demo<String>[ ]  demo =new Demo[10];
    }
}
Copy after login
  • 不能实例化类型变量

// 编译错误,需要类型参数需要确定类型Demo<T> demo = new Demo<T>
Copy after login
  • 泛型类的静态上下文中不能使用类型变量

public class Demo<T> {
    public static T name;    public static T getName() {
        ...
    }
}
Copy after login
  • 不能抛出也不能捕获泛型类的对象

//异常都是在运行时捕获和抛出的,而在编译的时候,泛型信息全都会被擦除掉。会导致这里捕获的类型一致try{  
}catch(Problem<Integer> e1){  
    //do Something... }catch(Problem<Number> e2){  
    // do Something ...}
Copy after login

 以上就是11.Java 基础 - 泛型的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!