1: Generics (master)
(1) Generic overview
It is a special type that defers to explicit types of work until the object is created or the method is called.
(2) Format:
< data types >
Note: This data type can only be a reference type.
(3) Benefits:
A: Advance the run-time problem to the compile period
B: Forced type conversions are avoided
C: Optimize the program design, solve the yellow warning line problem, make the program more secure
(4) Generics of past life
A: The origin of generics
When an object type is an arbitrary type, a transformation problem is implied when the transition is down.
B: Generic classes: Public class class name < generic type 1, generic type 2 、、、 > such as: public class studet<t>{//Inside T represents a type name. }
C: Generic method: (it does type inference)
Type 1: There is a generic type on the class.
public class Stduet<t> {
public void Method (T t) {}
}
Type 2: No generic type on class
public class students{
Public <T> void Show (T t) {}
}
D: Generic interface: Public interface interface name < generic type 1, generic type 2 、、、 >{}
public interface Ter<t t>{
public void Add (T t) {}
}
E: Generic high-level wildcard character:
Because the generic type must be the same before and after the specified type, the error is otherwise. Sometimes, we don't know whether it's the class or the subclass of the class, so it's just not possible to determine the type at first, so a wildcard is required.
? Represents any type, if not explicitly, object and any Java class
such as: collection<?> C =new arraylist<object> ();
? Extends e represents a down qualification, E and its subclasses, here is a representation that only wildcard the class and subclasses of that class can.
such as: collection<? Extends animal> C =new arraylist<dog> ();
collection<? Extends animal> C =new arraylist<object> (); No
? Super E represents an upward qualification, E and its parent class, which means that only the class and the parent class of the class can be wildcard.
such as: collection<? Extends animal> C =new arraylist<dog> (); No
collection<? Extends animal> C =new arraylist<object> (); OK
(5) New features of JDK5: Automatic disassembly box, generics, enhanced for, static import, variable parameters, enumeration.
2: Enhanced for loop
(1) is a For loop
(2) Format:
For (the element's data type variable name: An array or an object of the collection collection) {
With this variable, the variable is actually an array or an element in the collection.
}
(3) Benefits:
Simplifies the traversal of arrays and collections
(4) Disadvantages
The target of the enhanced for loop cannot be null. It is recommended that you determine whether null before use.
3: Static Import
(1) You can import to a method-level import, you can call the method name directly.
(2) Format:
Import static package name .... Class name. method name;
(3) Precautions:
A: The method must be static
B: If more than one class has a method with the same name, it is not good to differentiate, but also to add a prefix.
So generally we do not use static import, but must be able to understand.
4: Variable parameters (Master)
(1) If we are writing a method, the parameter number is ambiguous, we should define the variable parameter.
(2) Format:
Modifier returns a value type method name (data type ... Variable) {}
public static int sum (int ... a) {}//When the method is called, there will be a specific number of arguments.
Attention:
A: The variable A is actually an array name
B: If a method has more than one parameter and has a mutable parameter, the variable parameter must be in the last
(3) A method of arrays tool class
Aslist () turns the array into a collection.
Note: Although you can convert an array to a collection, the length of the collection cannot be changed because the collection is essentially an array, and the length of the array is constant.
Generics in Java