The role of generics
- Canonical data types, such as specifying the specific data types of a List Map.
new List<String>();
new Map<String,String>();
Copy the code
- As a placeholder, to increase versatility.
class IntSum{ int sum(int a,int b){ return a + b; } } class StringSum{ String sum(String a,String b){ return a + b; } } class Sum<T>{ T sum(T t1,T t2){ return t1 + t2; }}Copy the code
- Generic life cycle: In debug mode, generics are not checked in production mode. The goal is to help developers spot possible errors ahead of time.