java interview - Generics

1. What is a generic? What is the role of generics?

  Is parameterized generic type, the data type is operated is specified as a parameter. This parameter type may be used in the creation of classes, interfaces and methods, generic class, generic interfaces, generic methods are called.
  Before generics, an object can only be added to the collection of type object, when taken out use, must be cast, easily lead ClassCastException. Generics provide type safety compiler, make sure that you can only put objects into the collection of the correct type.

2, java generics How does it work? What is type erasure?

  Generics are implemented by type erasure compiler erased all types of related information at compile time, so any type of relevant information does not exist at runtime. For example List At runtime only one List to represent. The aim is to ensure they are capable and compatible with previous versions of the development java5 binary library. You can not be accessed at runtime to type parameters because the compiler has already converted to a generic type primitive type.

3. What is generic in the limited and non-limited wildcard wildcard?

  Is a wildcard types defined limits. There are two limited wildcard, one is <? extends T>, by ensuring type must be an upper limit to a subclass of T defined type. Another is <? Super T>, by ensuring a lower limit T type must be defined in the parent class type. Generic type must be initialized with types within the limit, otherwise it will cause a compiler error.
  Non-limiting wildcard for the generic type can be initialized with any type <?>.

Guess you like

Origin www.cnblogs.com/ssl-bl/p/11032472.html