Home  >  Article  >  Java  >  Understand in depth what are Java generics? How to use generics?

Understand in depth what are Java generics? How to use generics?

青灯夜游
青灯夜游forward
2018-10-19 17:36:164304browse

This article brings you an in-depth understanding of what Java generics are? How to use generics? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. What are generics

"Generics" means that the code written can be reused by different types of objects . Generics were proposed to write more reusable code. The essence of generics is parameterized type, that is to say, the data type is specified as a parameter .

For example, the common collection class LinkedList:

public class LinkedList extends AbstractSequentialList implements List,Deque,Cloneable,Serializable{
 //.....
 transient Link voidLink;
//.....    
}

You can see that LinkedList There is a special part after the class name and the interface name it implements,And the type of its members Link also contains a ,This symbol is a type parameter, which allows different types to be passed in when creating a LinkedList at runtime.

2. Why introduce generics

Introduction Before generics, to implement a general method that can handle different types, you need to use Object as attributes and method parameters, such as this:

public class Generic{
    private Object[] mData;

    public Generic(int capacity){
        mData = new Object[capacity];
    }
    
    public Object getData(int index){
       //.....
       return mData[index];
    }
     
    public void add(int index,Object item){
       //.....
       mData[index] = item;
    }   

}

It uses an Object array to Save the data so that you can add different types of objects when using it:

Generic generic = new Generic(10);
generic.add(0,"fangxing");
generic.add(1,23);

Object is the parent class of all classes, and all classes can be added to the above classes as members; when When it needs to be used, forced conversion must be performed, and this forced conversion is likely to cause a conversion exception:

String item1 = (String) generic.getData(0);
String item2 = (String) generic.getData(1);

The second line of code will force an Integer into a String, and an error will be reported during runtime:

Understand in depth what are Java generics? How to use generics?

It can be seen that using Object to implement general and different types of processing has the following two disadvantages:

  1. You need to cast it to the desired type every time you use it

  2. The compiler does not know whether the type conversion is normal during compilation. It only knows when it is run. It is unsafe.

According to the description in "Java Programming Thoughts", the motivation for the emergence of generics is:

There are many reasons that contributed to the emergence of generics, and the most fascinating One reason to pay attention is to create container classes.

After the emergence of generics in JDK 1.5, many collection classes use generics to save different types of elements, such as Collection:

public interface Collection extends Iterable{
   
     Iterator iterator();
   
     Object[] toArray();
     
      T[] toArray(T[] a);

     boolean add(E e);
     
     boolean remove(Object o);
 
     boolean  containsAll(Collecion> c);

     boolean addAll(Collection extends E> c);

     boolean removeAll(Collection> c);
   
}

actually introduced The main goals of generics are as follows:

##Type safety

  • The main goal of generics is to improve Java Type safety of the program

  • ClassCastException exceptions caused by incorrect Java types can be checked during compilation

  • The earlier the compliance, the smaller the cost of errors Principle

Eliminate forced type conversion

  • A side benefit of generics is that you can directly get the target type when using it. Eliminates many casts

  • What you get is what you need, which makes the code more readable and reduces the chance of errors

Potential Performance benefits

  • Due to the way generics are implemented, supporting generics requires (almost) no JVM or class file changes

  • All the work is done in the compiler

  • The code generated by the compiler is almost the same as the code written without using generics (and casts), but with better type safety That’s it

3. How to use generics

The essence of generics is

parameterized types, which means that the data type being operated on is specified as a parameter.

The meaning of the type parameter is to tell the compiler the type of instance to be stored in this collection, thereby prompting when adding other types, and ensuring type safety at compile time.

Parameter types can be used in the creation of classes, interfaces and methods, which are called generic classes, generic interfaces and generic methods respectively.

public class GenericClass{
    private F mContent;
    
    public GenericClass(F content){
      mContent = content;
    }
  
    /*
      泛型方法
   */
    public F getContent(){
      return mContent;
    }
    
    public void setContent(F content){
      mcontent = content;
    }
   
    /*
       泛型接口
    */
    public interface GenericInterface{
        void  doSomething(T t);
    }

}

泛型类  

泛型类和普通类的区别就是类名后有类型参数列表 ,既然叫“列表”了,当然这里的类型参数可以有多个,比如 public class HashMap,参数名称由开发者决定。

类名中声明参数类型后,内部成员、方法就可以使用这个参数类型,比如上面的 GenericClass 就是一个泛型类,它在类名后声明了类型 F,它的成员、方法就可以使用 F 表示成员类型、方法参数/返回值都是 F 类型。

 泛型类最常见的用途就是作为容纳不同类型数据的容器类,比如 Java 集合容器类。

泛型接口

和泛型类一样,泛型接口在接口名后添加类型参数,比如上面的 GenericInterface,接口声明类型后,接口方法就可以直接使用这个类型。

实现类在实现泛型接口时需要指明具体的参数类型,不然默认类型是 Object,这就失去了泛型接口的意义。

未指明类型的实现类,默认是 Object 类型:

public class Generic implements GenericInterface{
    @Override
    public void doSomething(Object o){
      //...
    }
}

指明了类型的实现:

public class Generic implements GericInterface{
     @Override
     public void doSomething(String s){
       //.....
     }
}

泛型接口比较实用的使用场景就是用作策略模式的公共策略, Comparator就是一个泛型接口:

public interface Comparator{
    public int compare(T lhs, Trhs);
    public bollean equals(Object object);
}

泛型接口定义基本的规则,然后作为引用传递给客户端,这样在运行时就能传入不同的策略实现类。

泛型方法

泛型方法是指使用泛型的方法,如果它虽在的类是一个泛型类,那就很简单了,直接使用类声明的参数。

如果一个方法所在的类不是泛型类,或者他想要处理不同于泛型类声明类型的数据,那它就需要自己声明类型。

/*
    传统的方法,会有unchecked ... raw type 的警告 
*/
public Set union(Set s1, Set s2){
    Set result = new HashSet(s1);
    result.addAll(s2);
    return result;
}

/*
    泛型方法,介于方法修饰符和返回值之间的称作 类型参数列表(可以有多个)
    类型参数列表 指定参数、返回值中泛型的参数类型范围,命名惯例与泛型相同。
*/
public  Set union2(Set s1, Set s2){
    Set result = new HashSet(s1);
    result.addAll(s2);
    return result;
}

四、泛型的通配符

通配符:传入的类型有一个指定的范围,从而可以进行一些特定的操作

泛型中有三种通配符形式:

1.>无限制通配符

2. extends E> extends 关键字声明了类型的上界,表示参数化的类型可能是所指定的类型,或者是此类型的子类。

3. super E> super 关键字声明了类型的下界,表示参数化类型可能是指定类型,或者是此类型的父类。

无限制通配符

要使用泛型,但是不确定或者不关心实际要操作的类型,可以使用无限制通配符(尖括号里一个问号,即 > ),表示可以持有任何类型。

? 和 Object 不一样,List> 表示未知类型的列表,而 List 表示任意类型的列表。

如传入个 List ,这时 List 的元素类型就是 String想要往 List 里添加一个 Object这当然是不可以的。

上界通配符

在类型参数中使用 extends 表示这个泛型中的参数必须是 E 或者 E 的子类,这样有两个好处:

  • 如果传入的类型不是 E 或者 E 的子类,编辑不成功

  • 泛型中可以使用 E 的方法,要不然还得强转成 E 才能使用

下界通配符

在类型参数中使用 super 表示这个泛型中的参数必须是 E 或者 E 的父类。

private  void add(List super E> dst, List Src){
    for (E e : src){
    dst.add(e);
   }
}

上面的 dst 类型 “大于等于” src 的类型,这里的“大于等于”是指 dst 表示的范围比 src 要大,因此装得下 dst 的容器也就能装 src。

通配符比较

无限制通配符 和 Object 有些相似,用于表示无限制或者不确定范围的场景。

用于灵活写入或比较,使得对象可以写入父类型的容器,使得父类型的比较方法可以应用于子类对象。

用于灵活读取,使得方法可以读取 E 或 E 的任意子类型的容器对象。

Understand in depth what are Java generics? How to use generics?

因此使用通配符的基本原则:

  • 如果参数化类型表示一个 T 的生产者,使用 ;(T 的子类)

  • 如果它表示一个 T 的消费者,就使用 ;(T 的父类)

  • 如果既是生产又是消费,那使用通配符就没什么意义了,因为你需要的是精确的参数类型。

小总结一下:

  • T 的生产者的意思就是结果会返回 T,这就要求返回一个具体的类型,必须有上限才够具体;

  • T 的消费者的意思是要操作 T,这就要求操作的容器要够大,所以容器需要是 T 的父类,即 super T;

举个例子:

private > E max(List extends E> e1){

    if(e1 == null){
        return null;
    }

    //迭代器返回的元素属于 E 的某个子类型
   
    Iterator extends E> iterator = e1.iterator();
    E result = iterator.next();
    while (iterator.hasNext()){
     E next = iterator.next();
      if(next.compareTo(result)>0){
       result = next;
      }
    }
    return result;
}

1.要进行比较,所以 E 需要是可比较的类,因此需要 extends Comparable(注意这里不要和继承的 extends 搞混了,不一样)

2.Comparable 要对 E 进行比较,即 E 的消费者,所以需要用 super

3.而参数 List 表示要操作的数据是 E 的子类的列表,指定上限,这样容器才够大

五、泛型的类型擦除

Java 中的泛型和 C++ 中的模板有一个很大的不同:

  • C++ 中模板的实例化会为每一种类型都产生一套不同的代码,这就是所谓的代码膨胀。

  • Java 中并不会产生这个问题。虚拟机中并没有泛型类型对象,所有的对象都是普通类。

在 Java 中,泛型是 Java 编译器的概念,用泛型编写的 Java 程序和普通的 Java 程序基本相同,只是多了一些参数化的类型同时少了一些类型转换。

实际上泛型程序也是首先被转化成一般的、不带泛型的 Java 程序后再进行处理的,编译器自动完成了从 Generic Java 到普通 Java 的翻译,Java 虚拟机运行时对泛型基本一无所知

当编译器对带有泛型的java代码进行编译时,它会去执行类型检查和类型推断,然后生成普通的不带泛型的字节码,这种普通的字节码可以被一般的 Java 虚拟机接收并执行,这在就叫做 类型擦除(type erasure)。

实际上无论你是否使用泛型,集合框架中存放对象的数据类型都是 Object,这一点不仅仅从源码中可以看到,通过反射也可以看到。

List strings = new ArrayList();
List integers = new ArrayList();
System.out.println(Strings.getClass()==integers.getClass());//true

上面代码输出结果并不是预期的false,而是true。其原因就是泛型的擦除。

六、擦除的实现原理

一直有个疑问,Java 编译器在编译期间擦除了泛型的信息,那运行中怎么保证添加、取出的类型就是擦除前声明的呢?

Java 编辑器会将泛型代码中的类型完全擦除,使其变成原始类型。当然,这时的代码类型和我们想要的还有距离,接着 Java 编译器会在这些代码中加入类型转换,将原始类型转换成想要的类型。这些操作都是编译器后台进行,可以保证类型安全。总之泛型就是一个语法糖,它运行时没有存储任何类型信息。

擦除导致的泛型不可变性

泛型中没有逻辑上的父子关系,如 List 并不是 List 的父类。两者擦除之后都是List,所以形如下面的代码,编译器会报错:

/*
    两者并不是方法的重载,擦除之后就是同一方法,所以编译不会通过。
    擦除之后:
    void m(List numbers){}
    void m(List Strings){}   //编译不通过,已经存在形同方法签名 
*/
   void method(List numbers){}
   void method(List strings){}

泛型的这种情况称为 不可变性,与之对应的概念是 协变、逆变:

  • 协变:如果 A 是 B 的父类,并且 A 的容器(比如 List) 也是 B 的容器(List)的父类,则称之为协变的(父子关系保持一致)

  • 逆变:如果 A 是 B 的父类,但是 A 的容器 是 B 的容器的子类,则称之为逆变(放入容器就篡位了)

  • 不可变:不论 A B 有什么关系,A 的容器和 B 的容器都没有父子关系,称之为不可变

Java 中数组是协变的,泛型是不可变的。

擦除的拯救者:边界

我们知道,泛型运行时被擦除成原始类型,这使得很多操作无法进行.

如果没有指明边界,类型参数将被擦除为 Object。

如果我们想要让参数保留一个边界,可以给参数设置一个边界,泛型参数将会被擦除到它的第一个边界(边界可以有多个),这样即使运行时擦除后也会有范围。

比如:

public class GenericErasure {
    interface Game{
      void play();
    }
    interface Program{
      void code();
   }
   public static calss People{
       private T mPeople;
       
       public People(T people){
         mPeople = people;
       }
       public void habit(){
         mPeople.code();
         mPeople.play();
        }
   }
   

}

上述代码中, People 的类型参数 T 有两个边界,编译器事实上会把类型参数替换为它的第一个边界的类型。

七、泛型的规则

  • 泛型的参数类型只能是类(包括自定义类),不能是简单类型。

  • 同一种泛型可以对应多个版本(因为参数类型是不确定的),不同版本的泛型类实例是不兼容的。

  • 泛型的类型参数可以有多个

  • 泛型的参数类型可以使用 extends 语句,习惯上称为“有界类型”

  • 泛型的参数类型还可以是通配符类型,例如 Class

泛型的使用场景

当类中要操作的引用数据类型不确定的时候,过去使用 Object 来完成扩展,JDK 1.5后推荐使用泛型来完成扩展,同时保证安全性。

八、总结

1.上面说到使用 Object 来达到复用,会失去泛型在安全性和直观表达性上的优势,那为什么 ArrayList 等源码中的还能看到使用 Object 作为类型?

      泛型出现时,Java 平台即将进入它的第二个十年,在此之前已经存在了大量没有使用泛型的 Java 代码。人们认为让这些代码全部保持合法,并且能够与使用泛型的新代码互用,非常重要。

这样都是为了兼容,新代码里要使用泛型而不是原始类型。

2.泛型是通过擦除来实现的。因此泛型只在编译时强化它的类型信息,而在运行时丢弃(或者擦除)它的元素类型信息。擦除使得使用泛型的代码可以和没有使用泛型的代码随意互用。

3.如果类型参数在方法声明中只出现一次,可以用通配符代替它。

private  void swap(List list, int i, int j){
    //....
}

只出现了一次 类型参数,没有必要声明,完全可以用通配符代替:

private void swap(List> list, int i, int j){
    //...
}

对比一下,第二种更加简单清晰吧。

4.数组中不能使用泛型

Array 事实上并不支持泛型,这也是为什么 Joshua Bloch 在 《Effective Java》一书中建议使用 List 来代替 Array,因为 List 可以提供编译期的类型安全保证,而 Array 却不能。

5.Java 中 List 和原始类型 List 之间的区别?

  • 在编译时编译器不会对原始类型进行类型安全检查,却会对带参数的类型进行检查

  • 通过使用 Object 作为类型,可以告知编译器该方法可以接受任何类型的对象,比如String 或 Integer

  • 你可以把任何带参数的类型传递给原始类型 List,但 却不能把 List 传递给接受 List 的方法,因为泛型的不可变性,会产生编译错误。

九、补充

静态资源不认识泛型

接上一个话题,如果把去掉,那么:

private static T ifThenElse(boolean b, T first, T second){
    return b ? first : second;
}

报错,T未定义。但是如果我们再把static去掉:

public class TestMain{
    public static void main(String[] args){}
    
   @SuppressWarnings("unused")
   private List ifThenElse(boolean b,T first, T second){
     return null;
   }
}

这并不会有任何问题。两相对比下,可以看出static方法并不认识泛型,所以我们要加上一个,告诉static方法,后面的T是一个泛型。既然static方法不认识泛型,那我们看一下static变量是否认识泛型:

public class TestMain{
    private List notStaticList;
    private static List staticList;
}

这证明了,static变量也不认识泛型,其实不仅仅是staic方法、static变量、static块,也不认识泛型,可以自己试一下。总结起来就是一句话:静态资源不认识泛型。

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study. For more related tutorials, please visit Java video tutorial, java development graphic tutorial, bootstrap video tutorial!

The above is the detailed content of Understand in depth what are Java generics? How to use generics?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete