Detailed explanation of generic instances in Java
I. Introduction:
In Java se 1 5. Before that, in the absence of generics, the "arbitrary" of parameters was realized by referencing the type object. The disadvantage of "arbitrary" is that it requires explicit forced type conversion, which requires developers to predict the actual parameter types. In case of cast errors, the compiler may not prompt errors, and exceptions occur only when running, which is a security risk.
The advantage of generics is to check type safety at compile time, and all coercions are automatic and implicit, so as to improve the reuse rate of code.
2、 Generic parameters:
output:
The actual type of T is: Java Lang. integer value = 100 -------------------------------- the actual type of T is: Java lang.String value= Hello Dylan!
3、 Generic class:
output:
strFoo. getX=Hello Generics! douFoo. getX=33.0 objFoo. getX=java. lang. Object@1d0fafc
IV. restricted generics:
Output: instantiation succeeded!
(V) generic methods:
output: java. lang.String java. lang.Integer java. lang.Character GenericFunction
----------------------------- dylan presents.
summary
The above is all about the detailed explanation of generic instances in Java. I hope it will be helpful to you. If there are deficiencies, please leave a message to point out. Thank you for your support!