CodePudding user response:
Use generic type parameters can ensure consistent ah, just like you use collection, do you think what are the benefits of using a generic collections? If you set to specify a generic String, to save time can only save String, take the time do not need to be strong is also a String, or you can take a receiving method of generic parameters, see if I can according to the concrete methods to explain to you,CodePudding user response:
With type object directly, you need to judge whether the type of the actual quote is consistent, also forced conversion, etc., the types of code trival,For example, compare the method test1 and test2
The class AA extends A {
Public void action () {
System. The out. Println (" AA ");
}
}
Public class A {
Public void action () {
System. The out. Println (" A ");
}
Public static & lt; T extends A> Void test1 (T T) {//T is A subclass of generic
T.a ction ();//code concise
}
Public static void test2 (Object t) {//Object code cumbersome
If t instanceof (A) {//judgment type
(t) (A). The action ();//forced conversion
}
}
Public static void main (String [] args) {
A=new A (AA);
Test1 (a);
Test2 (a);
}
}
CodePudding user response:
Generics are exposing mistakes this morning at compile time, and generic also will type erasure at run time, in order to be forward compatible, in fact, you can use reflection to bypass generic detection, but you will need to write at runtime instanceof such judgment,CodePudding user response:
Generic parameter type is to limit the effect of parameters, such aspublic & lt; T> Void function (T, t1, t2 T) {
.
}
This method limits the two parameters must be the same type, the Object can't do that,
CodePudding user response:
A generic type is the essence of erasures, at compile time will automatically convert incoming type for you, this is the method simplifies the overloading, the use of the object can be, but the space of the relative waste is largerCodePudding user response:
Generics can avoid strong type to turn the trouble, will the run-time ClasscastException, are evident at compile time,
The Collection coll=new ArrayList ();
Coll. Add (" ABC ");
Coll. Add (" itcast ");
Coll. Add (5);//due to set no limit, can be any type to contain
The Iterator it=coll. The Iterator ();
While (it. HasNext ()) {
//need to print the length of each String, then the iteration of object into a String type
String STR=(String) it. Next ();
System. Out. Println (STR. The length ());
}
public static void main (String [] args) {
CollectionList=new ArrayList (a);
List. The add (" itheima ");
List. The add (" itcast ");
//list. Add (666);//when set clear type, storage type inconsistent will compile error
//collection has specific storage element type, so when using iterators, the iterator can also be know specific traversal
The element type
IteratorIt=list. The iterator ();
While (it. HasNext ()) {
String STR=it. Next ();
//when using IteratorControl the element type, you don't need strong turned, access to the element directly is
Type String
System. Out. Println (STR. The length ());
}
}
CodePudding user response:
Generic itself is used to solve the problem of Object downward transition,When you use the Object to receive the Object, you will find that you cannot directly use Object to access the content, you need to transition to down,