Java Generics 简明教程

Java Generics - Overview

如果能够编写一个单一的 sort 方法对 Integer 数组、String 数组或支持排序的任何类型的数组中的元素进行排序,那就太好了。

It would be nice if we could write a single sort method that could sort the elements in an Integer array, a String array, or an array of any type that supports ordering.

Java 泛型方法和泛型类使程序员能够使用单个方法声明指定一组相关方法,或者使用单个类声明指定一组相关类型。

Java Generic methods and generic classes enable programmers to specify, with a single method declaration, a set of related methods, or with a single class declaration, a set of related types, respectively.

泛型还提供编译时类型安全性,使程序员能够在编译时捕捉无效的类型。

Generics also provide compile-time type safety that allows programmers to catch invalid types at compile time.

使用 Java 泛型概念,我们可以为对象数组编写一个泛型方法,然后使用 Integer 数组、Double 数组、String 数组等调用泛型方法,对数组元素进行排序。

Using Java Generic concept, we might write a generic method for sorting an array of objects, then invoke the generic method with Integer arrays, Double arrays, String arrays and so on, to sort the array elements.