1. Why are there generics ?
Early Object Type can accept any object type , But in practical use , There will be the problem of type conversion , There are hidden dangers , therefore Java Provide generics to solve this security problem
2. What is generics ?
generic paradigm , Namely “ Parametric type ”. When it comes to parameters , The most familiar thing is to define methods with formal parameters , Then the arguments are passed when calling this method . So what about parameterized types ? seeing the name of a thing one thinks of its function , That is to parameterize the type from the original concrete type , Similar to variable parameters in methods , The type is also defined as a parameter ( It can be called type parameter ), And then in use / The specific type is passed in when calling ( type argument ).
The essence of generics is to parameterize types ( Without creating a new type , Different types specified by generics are used to control the types of formal parameter specific restrictions ). That is to say, in the process of using generics , The data type of the operation is specified as a parameter , This parameter type can be used in classes , Interface and method , They are called generic classes , generic interface , generic method .
3. The use of generics
Generics are used in three ways , They are : Generic class , generic interface , generic method
(1) Generic class
Define generics on classes
format :public class Class name < generic types ,…>
be careful : generic types Must be a reference type
// A generic class public class ObjectTool<E> { private E obj; public E getObj() { return
obj; } public void setObj(E obj) { this.obj = obj; } } // Generic class testing public class
ObjectToolDemo { public static void main(String[] args) { ObjectTool<String>
obj=new ObjectTool<String>(); obj.setObj(" Zhang San "); String s=obj.getObj();
System.out.println(s); } }
(2) generic method
Define generics on Methods
format :public< generic types > Return type Method name ( generic types )
// generic method public class ObjectTool{ public <T> void show(T t) { } } // test public
class ObjectToolDemo { public static void main(String[] args) { ObjectTool
obj=new ObjectTool(); obj.show(1); obj.show("hello"); obj.show(true); } }
(3) generic interface
Define generics on interfaces
format :public interface Interface name < generic types >
// Defining generic interfaces public interface Inter<T> { public abstract void show(T t); } // Implementation interface
public class InterfaceImpl<T> implements Inter<T>{ public static void
main(String[] args) { } public void show(T t) { } } // test public class
InterfaceDemo { public static void main(String[] args) { // Interface test Inter<String>
i=new InterfaceImpl<String>(); i.show("hello"); Inter<Integer> i2=new
InterfaceImpl<Integer>(); i2.show(100); Inter<Boolean> i3=new
InterfaceImpl<Boolean>(); i3.show(true); } }
4. Benefits of generics
Java
The introduction of generics is a big enhancement . It's not just language , The type system and compiler have changed a lot , To support generics , And the class library has been greatly renovated , So many important classes , For example, set framework , Has become generic .
1, Type safety . The main goal of generics is to improve Java
Type security of programs . By knowing the type restrictions of variables defined using generics , The compiler can verify type assumptions to a much higher degree . No generics , These assumptions only exist in the minds of programmers ( Or if you're lucky , It also exists in code comments ).
5. Generic wildcards
* Generic wildcards <?>
Any type , If not clear , So that's it Object And arbitrary Java class // When generics are explicitly written , It needs to be consistent Collection<Object>
c1=new ArrayList<Object>(); //Collection<Object> c2=new ArrayList<Animal>();
//Collection<Object> c3=new ArrayList<cat>(); report errors //Collection<Object> c4=new
ArrayList<dog>(); //? Indicates that any type is OK Collection<?> c2=new ArrayList<Animal>();
Collection<?> c3=new ArrayList<cat>(); Collection<?> c4=new ArrayList<dog>();
* ? Extends E
Set down ,E And its subclasses //? Extends E Collection<? extends Animal> c5=new
ArrayList<Animal>(); Collection<? extends Animal> c6=new ArrayList<dog>();
Collection<? extends Animal> c7=new ArrayList<cat>();
* ? super E
Set up ,E And its parent class // Set up ,E And its parent class Collection<? super Animal> c8=new
ArrayList<Object>(); Collection<? super Animal> c9=new ArrayList<Animal>();
Technology
Daily Recommendation