The benefit of 1:java generics is that type checking can be implemented, which is more secure, and can be extended and enhanced by functions.
2: The concept of generics is a parameterized type, equivalent to the formal parameters in the method, first make a placeholder and then in use when the real parameters are passed in.
3:
Packagegeneric;/*** Custom generic class *@authorPC **/ Public classGenericmodel <T>{ PrivateT data; PublicGenericmodel () {} PublicGenericmodel (T data) { This. data =data; } Public voidsetData (T data) { This. data =data; } PublicT GetData () {return This. Data; }}
Packagegeneric; Public classGenerictest { Public Static voidMain (String[]args) {Genericmodel<string>genericmodel =NewGenericmodel<string>(); Genericmodel.setdata ("11"); System.out.println ("String Data" +genericmodel.getdata ()); Genericmodel<integer>genericmodel1 =NewGenericmodel<integer>(); Genericmodel1.setdata (22); System.out.println ("Integer Data" +genericmodel.getdata ()); /*** found the output of the following two, this shows that at the time of real implementation, the class has only one type, and there are no two different types of classes * when using generic classes, although two type parameters, but actually in memory only one type of parameter * to investigate its cause, Is that the concept of generics in Java is intended to cause it to work only in the code compilation phase, and during compilation, when the generic results are correctly validated, the relevant information for generics is erased, * that is, the class file after the successful compilation is not a package containing any generic information. Generic information is not entered into the run-time phase. * * * Generic types are logically viewed as multiple different types, and are essentially the same basic types. * */System.out.println (Genericmodel.getclass ()); System.out.println (Genericmodel1.getclass ()); }}
JDK Learning Path---java generics