[Previous words] recently, my face has been dry and I need to buy some cosmetics. Java generics have a good learning experience. [Definition] 1. There are two main types of generic definitions: Some types containing type parameters in program encoding, that is, generic parameters can only represent classes, it cannot represent individual objects. (This is a common definition today) Some Classes Containing Parameters in program encoding. Its parameters can represent classes or objects. (Most people now call this a template) regardless of the definition, generic parameters must be specified when they actually use generics. Ii. Objective of Using Generics: some strong-type programming languages Support generics. The main objective is to enhance type security and reduce the number of class conversions, however, some programming languages that support generics can only achieve some purpose. Generic Program Design (Generic programming) means that the code can be reused by many different types of objects. Is an extension of the Type System of the java language to support creating classes that can be parameterized by type. You can regard the type parameter as a placeholder of the type specified when parameterized type is used, just as the formal parameter of the method is a placeholder that is passed during runtime. [Several types of Java Generic Code] 1. Without generic code, we define a Person class, which contains three attributes: x, y, and z. At the beginning of definition, we do not know what these three attributes are for, so we define them as the Object type. However, in use, we assign the int, double, and String types to x, y, and z respectively, we need to forcibly convert these three types of values. Code 1. Person. java: Copy code 1 public class Person {2 private Object x; 3 private Object y; 4 private Object z; 5 // use the Object type. Can be converted to any type 6 public Object getX () {7 return x; 8} 9 public void setX (Object x) {10 this. x = x; 11} 12 public Object getY () {13 return y; 14} 15 public void setY (Object y) {16 this. y = y; 17} 18 public Object getZ () {19 return z; 20} 21 public void setZ (Object z) {22 this. z = z; 23} 24} copy Code 2. noGenericTest. java copy code 1 public class NoGenericTest {2 public static void main (String [] args) {3 Person boy = n Ew Person (); 4 boy. setX (20); 5 boy. setY (22.2); 6 boy. setZ ("handsome guy TT"); 7 // here we need to force type conversion based on different types of values set. 8 int x = (Integer) boy. getX (); 9 double y = (double) boy. getY (); 10 String z = (String) boy. getZ (); 11 12 System. out. println (x); 13 System. out. println (y); 14 System. out. println (z); 15} 16} copy code 3. run result 1 202 22.23 handsome guy TT 2. Use a type variable generic code we define a generic class Person and define three attributes x, y, z. In the test class, set the attribute value and print it. 1. person. java copy code 1 public class Person <T> {2 private T x; 3 private T y; 4 private T z; 5 public T getX () {6 return x; 7} 8 public void setX (T x) {9 this. x = x; 10} 11 public T getY () {12 return y; 13} 14 public void setY (T y) {15 this. y = y; 16} 17 public T getZ () {18 return z; 19} 20 public void setZ (T z) {21 this. z = z; 22} 23} copy Code 2. genericTest. java copy code 1 public class GenericTest {2 publ Ic static void main (String [] args) {3 Person boy = new Person (); 4 boy. setX (20); 5 boy. setY (22.2); 6 boy. setZ ("Handsome TT"); 7 // type conversion is not required 8 System. out. println (boy. getX (); 9 System. out. println (boy. getY (); 10 System. out. println (boy. getZ (); 11} 12} copy code 3. run result 1 202 22.23 handsome guy TT 3. Use two types of variable generic code we define a generic class Person and define two attributes x, y, two different types of variables are used. In the test class, we set the attribute value and print it. 1. person. java copy code 1 public class Person <T1, T2> {2 private T1 x; 3 private T2 y; 4 public T1 getX () {5 return x; 6} 7 public void setX (T1 x) {8 this. x = x; 9} 10 public T2 getY () {11 return y; 12} 13 public void setY (T2 y) {14 this. y = y; 15} 16} copy Code 2. genericTest. java copy code 1 public class GenerricTest {2 public static void main (String [] args) {3 Person <String, Integer> boy = new Person <String, I Nteger> (); 4 boy. setX ("Handsome TT"); 5 boy. setY (20); 6 System. out. println (boy. getX (); 7 System. out. println (boy. getY (); 8} 9 10} copy code 3. run result 1 handsome guy TT2 20 4. use generic inheritance we define a generic class Person, define two attributes x and y, then define another generic class Boy, define the attribute z, boy inherits the Person class. In the test class, we set the property value and print it. 1. person. java copy code 1 public class Person <T1, T2> {2 private T1 x; 3 private T2 y; 4 public T1 getX () {5 return x; 6} 7 public void setX (T1 x) {8 this. x = x; 9} 10 public T2 getY () {11 return y; 12} 13 public void setY (T2 y) {14 this. y = y; 15} 16} copy Code 2. boy copy code 1 public class Boy <T1, T2, T3> extends Person <T1, T2> {2 private T3 z; 3 public T3 getZ () {4 return z; 5} 6 public void setZ (T3 z) {7 this. z = Z; 8} 9} copy code 3. genericTest. java copy code 1 public class GenericTest {2 public static void main (String [] args) {3 Boy <String, Integer, Double> boy = new Boy <String, Integer, double> (); 4 boy. setX ("Handsome TT"); 5 boy. setY (20); 6 boy. setZ (200000.22); 7 8 System. out. println (boy. getX (); 9 System. out. println (boy. getY (); 10 System. out. println (boy. getZ (); 11} 12} copy code 4. run result 1 handsome guy TT2 203 200000.22 5. Use the generic interface to define a generic Type interface Person, defines two methods, and then defines another generic class Boy, implements the generic interface Person, defines the attributes x, y, z, in the test class, we set the attribute value, and print. 1. person. java 1 public interface Person <T1, T2> {2 public T1 getX (); 3 public T2 getY (); 4} 2. boy copy code 1 public class Boy <T1, T2, T3> implements Person <T1, T2> {2 private T1 x; 3 private T2 y; 4 private T3 z; 5 public T1 getX () {6 return x; 7} 8 public void setX (T1 x) {9 this. x = x; 10} 11 public T2 getY () {12 return y; 13} 14 public void setY (T2 y) {15 this. y = y; 16} 17 public T3 getZ () {18 return Z; 19} 20 public void setZ (T3 z) {21 this. z = z; 22} 23 24} copy code 3. genericTest. java copy code 1 public class GenericTest {2 public static void main (String [] args) {3 Boy <String, Integer, Double> boy = new Boy <String, Integer, double> (); 4 boy. setX ("Handsome TT"); 5 boy. setY (20); 6 boy. setZ (200000.22); 7 System. out. println (boy. getX (); 8 System. out. println (boy. getY (); 9 System. out. println (boy. getZ (); 10} 11} copy code 4. Run result 1 handsome guy TT2 203 200000.22 6. Use the generic method to describe that when defining a generic method, you must add a <T> before the returned value to declare that this is a generic method, only when a generic T is held can the generic T be used as the return value of the method. Define a common class Person and define a generic method. The following code: 1. person. java copy code 1 public class Person {2 public static <T> T getMiddle (T [] a) {3 return a [. length/2]; 4} 5 public static void main (String [] args) {6 String [] name = {"handsome guy TT", "handsome guy TT1 ", "Handsome TT2"}; 7 String middle = Person. <String> getMiddle (name); 8 System. out. println (middle); 9 10 Integer [] num = {20, 22, 25}; 11 Integer middle1 = Person. <Integer> getMiddle (num); 12 System. out. println (Middle1); 13 14 Double [] num1 = {20.0, 22.2, 25.5}; 15 Double middle2 = Person. <Double> getMiddle (num1); 16 System. out. println (middle2); 17} 18} copy Code 2. run result 1 handsome guy TT12 223 22.2 VII. The following Code limits the type variables. In method min, we define a variable smallest type as T, this shows that smallest can be an object of any class. We need to use the compareTo method in the following code, but we cannot determine that our T contains the CompareTo method, therefore, we need to limit T and let T inherit the Comparable class in the code. 1 public static <T extends Comparable> T min (T [] a) 1. person. java copy code 1 public class Person {2 public static <T extends Comparable> T min (T [] a) {3 if (a = null |. length = 0) {4 return null; 5} 6 T smallest = a [0]; 7 for (int I = 1; I <. length; I ++) {8 if (smallest. compareTo (a [I])> 0) {9 smallest = a [I]; 10} 11} 12 return smallest; 13} 14 public static void main (String [] args) {15 Integer [] num = {20, 25, 30, 10}; 16 I Nteger middle = Person. <Integer> min (num); 17 System. out. println (middle); 18} 19} copy Code 2. result 1 10 [Java generic understanding] 1. The primary prerequisite for correct understanding of generic concepts is understanding type erasure ). In Java, generics are basically implemented at the compiler level. The generated Java Byte Code 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. For example, the List <Object> and List <String> types defined in the Code will be changed to List after compilation. What JVM sees is only List, and the type information appended by generics is invisible to JVM. The Java compiler tries its best to discover possible errors during compilation, but it still cannot avoid type conversion exceptions at runtime. Many of the strange features of generics are related to the existence of this type erasure, including: generic classes do not have their own Class objects. For example, there is no List <String>. class or List <Integer>. class, but only List. class. Static variables are shared by all instances of the generic class. For Classes declared as MyClass <T>, the method for accessing the static variables in the class is still MyClass. myStaticVar. Objects Created through new MyClass <String> or new MyClass <Integer> share a static variable. Generic Type parameters cannot be used in catch statements for Java exception handling. Because exception handling is performed by JVM at runtime. Because the type information is erased, JVM cannot distinguish two exception types: MyException <String> and MyException <Integer>. For JVM, they are all of the MyException type. The catch statement corresponding to the exception cannot be executed. 2. Best practices can follow some basic principles when using generics to avoid some common problems. Avoid mixing generic classes and original types in code. For example, List <String> and List should not be used together. This will generate some compiler warnings and potential runtime exceptions. When you need to use the legacy code developed before JDK 5 and have to do so, try to isolate the relevant code as much as possible. When using generic classes with wildcards, you must specify the concept of a group of types represented by wildcards. Because the specific type is unknown, many operations are not allowed. It is best not to use the same Array for generic classes. You can only create a new List <?> [10] The new List <String> [10] cannot be created. This limits the ability to use arrays, and brings a lot of confusing problems. Therefore, you can use the collection class when you need functions similar to arrays. Do not ignore the warning information provided by the compiler.