Generics
What is a generic
Generics: a wide range of generic type
Beginning not sure what type, when in use, to determine what type
(1) At the beginning of the definition, leaving a jack
(2) when the object is created, again inserted into the corresponding type
Generics can be understood as "parameterized type." Mention of parameters, the most familiar with is visible when defining method parameters, and then call this method when passing arguments.
As the name suggests, is a specific type as a parameter of the original, similar to the process variable parameter types are also defined at this time in the form of parameters (type parameter may be referred to), and particularly when using the incoming / calling type (type arguments).
Generic for parameterized types (not created in case of a new type, to control the type parameter particularly limited by different types of generic specified). That is the generic use, the data type of the operation is specified as a parameter, which can be used in the type of classes, interfaces and methods, are called generic class, generic interfaces, generic method.
Generic definitions
Generic class: public class Demo
Generic interface: public interface ImplDemo <T, V> {}, and define the same class (the interface is a special class).
Generic method: public
<> Brackets, the name may be any commonly used T: Type, E: Element, K: Key, V: Value.
Code template type of uncertainty, who call the sections of the code, and anyone can to indicate this type.
If you do not specify a generic type, which is the default type Object
Generic benefits
Determine (1) compile-time type, type of security guarantee, to avoid the type conversion exception.
(2) to avoid the cast.
(3) Code beneficial reuse, increased versatility.
Generic nature
Generics is actually a syntactic sugar, essence, or Object, but it is still to do strong internal transfer.
class Demo<T> {
T x;
public Demo(T x) {
this.x = x;
}
public T getX() {
return x;
}
}
public class Test {
public static void main(String[] args) {
Demo<String> demo = new Demo<>("SSS");
String str = demo.x;
System.out.println(str);
}
}
Decompile after:
class Demo
{
Object x;
public Demo(Object x)
{
this.x = x;
}
public Object getX()
{
return x;
}
}
public class Test
{
public Test()
{
}
public static void main(String args[])
{
Demo demo = new Demo("SSS");
String str = (String)demo.x;
System.out.println(str);
}
}
Generic method and generic class
(1) generic class
Generic class defined above, class class name
Generic generic class which is defined only by the above conventional method, the above can not be used in a static method.
(2) a generic method
In the above method is added generics, at the time of use, to determine the specific type of parameter passing.
The method you want to use generics alone, there must be meaningful parameters.
class Demo {
public <T> void demo1(T t) {
System.out.println(t.getClass());
}
public <T> T demo2(T t) {
System.out.println(t.getClass());
return t;
}
public static <T> void demo3(T t) {
System.out.println(t.getClass());
}
}
public class Test {
public static void main(String[] args) {
Demo demo = new Demo();
demo.demo1("str");// class java.lang.String
demo.demo1(10);// class java.lang.Integer
demo.demo1(10.5);// class java.lang.Double
Demo.demo3(true);// class java.lang.Boolean
}
}
Generic wildcards
Wildcard: I do not know what type to use when received, can be used? Indicating an unknown.
(1) wildcard borderless
The main role of unbounded wildcard is to allow generics to accept an unknown type of data.
public static void printList(List<?> list) {
for (Object o : list) {
System.out.println(o);
}
}
public static void main(String[] args) {
List<String> l1 = new ArrayList<>();
l1.add("aa");
l1.add("bb");
l1.add("cc");
printList(l1);
List<Integer> l2 = new ArrayList<>();
l2.add(11);
l2.add(22);
l2.add(33);
printList(l2);
}
This use List <?> Way is the parent class reference point to a subclass object. Note that this method can not be written as the printList public static void printList (List