A Brief Introduction to Java Generics

Starting from jdk1.5, java allows the definition of generic classes, generic interfaces, and generic methods. Some interfaces and classes in Java AP widely use generics, such as
java.lang.Comparable interface is defined as Comparable<T> generic type interface

what is generic

In the coding stage, our programmers can use some specific characters (E, T, V) to replace the attribute type of the class, the parameter type of the method, the return value type of the method...etc.; then in the compiler stage, the specific characters will be used type to replace it
Example:

public class UsingGenericType<T> {
    
    
	// 定义一个泛型属性
	private T a;
	
	// 定义泛型参数
	public void setA(T a){
    
    
		this.a = a;
	} 
	
	public T getA(){
    
    
		return this.a;
	}

	public static void main(String[] args){
    
    
		// 实例化泛型类(用具体的类型代替泛型)
		UsingGenericType<Integer> ugt = new UsingGenericType();
	}
}

what's the use of generics

The main advantage of using generics is the ability to catch errors at compile time rather than run time ; generic classes or methods allow the programmer to specify objects of the type that will work with those classes or methods. If an attempt is made to use an incompatible object, the compiler This error will be detected. Using generics can improve the readability of the code, improve the reliability and maintainability of the software. Using generics can greatly reduce our workload in some tasks. It is safe and efficient

Define generic classes and generic interfaces

After the generic declaration is after the class name , wildcards can be used to specify the scope of the generic

//为类或者接口定义泛型,当使用时必须指定具体的类型
public class GenericStack<T>{
    
    
	//这里是使用GenericStack声明的泛型T, 而不是另外声明的泛型T
	private ArrayList<T> list = new ArrayList();
}

public interface Readable <T>{
    
    }
// 当使用时必须指定具体的类型
public class PersonInfo implements Readable<Integer>{
    
    }
generic method

Before the generic declaration returns a value , wildcards can be used to specify the scope of the generic

public class Anonymous{
    
    
	public <T> void test(T t){
    
    
		//业务逻辑
	}
}

//使用 由于T为非受限通配符,可以使用任意类型, 
//这里可以使用1 (int)的原因是编译器做了自动装包int转换成Integer
public static void main(String[] args){
    
    
	new Anonymous.test(1);
}

Generic wildcards

You can use 非受限通配, 受限通配, 下限通配to specify a range of
unrestricted wildcards for a generic type : Indicates that the specific type can be any type

//定义类泛型,在使用时T可以具体为任意类型,即为非受限类型
public class Anonymous<T>{
    
    }

Restricted wildcard : Indicates that the generic type can only be "realized" by a subclass of a specific type or itself

// extends Number:表示T必须是继承Number类.即代替T时,只能使用Number或者Number的子类
public class Anonymous<T extends Number> {
    
    }

Lower limit wild card : Indicates that the generic type can only be "realized" by the parent class of the specific type or itself

// super Integer: 表示T必须是Integer的父类或者本身
public class Anonymous< T super Integer> {
    
    }

Generic method declarations like

type erasure

The principle of generic implementation: Type erasure
Generic related information can be used by the compiler, but not available at runtime. This is called type erasure.
Generics exist at compile time. When the compiler confirms that generics are safe , it will be converted to a native type
such as:

ArrayList<String> list = new ArrayList();
list.add("mango");
String fruit = list.get(0);

//运行时的实际代码
ArrayList list = new ArrayList();
list.add("mango");
String fruit = (String) list.get(0);
// 所以运行时是不知道的泛型信息的

Another point, no matter what the actual concrete type is, the generic type is shared by all its instances.
ArrayList<String> list = new ArrayList(); ArrayList
<Integer> list2 = new ArrayList();
When ArrayList<String> and ArrayList<Integer> are two types, but at runtime, only one ArrayList is loaded into the JVM
System.out.println(list instanceof ArrayList); //true
System.out.println(list2 instanceof ArrayList ); //true

Generic usage restrictions

  1. Objects cannot be instantiated with new E()
  2. Arrays cannot be instantiated with new E[]
  3. A parameter of a class is not allowed to be a generic type in a static context
  4. Exception type cannot be generic
  5. The specific type of the generic type must be a reference type, and basic types such as int, char... cannot be used

Guess you like

Origin blog.csdn.net/qq_29757633/article/details/108137706