Simple Java Generics summary

Source: Internet
Author: User

  1. Defining generic Classes
    • The elements in a generic class should be generic types
    • You can instantiate a generic object with a specific substitution type variable
  2. Generic methods
    • Public static<t> t Getmiddle (t ... a), so form
    • Call generic method: String middle = arrayalg.<string>getmiddle ()
    • Most of the time, you can ignore <String>, ie arrayalg.getmiddle ()
  3. Qualification of type variables
    • If you need to call the data type of a particular interface, you need to use the following form
    • Public static<t extends comparable> T min (t[] a)
    • It is important to note that T is a sub-type of something behind it, which can be an interface or a class
    • Qualifiers are segmented using &, for example T extends comparable&serializable
    • You can inherit multiple interfaces and a superclass, and if you have a superclass, put the superclass in the first place.
  4. Generic code and virtual machines
    1. Type parameter Erase
      • If the variable is not qualified, then the original type is an object
      • Place an interface with no methods at the end of the boundary list
    2. Translating generic expressions
      • If the return type is erased, the compiler automatically inserts a strong
      • Pair<employee> buddies= ...
      • Employee Buddy = Buddies.getfirst
      • The above call to Pair.getfirst is called in the virtual machine, casting the returned object class to the employee type
    3. Translating generic methods
      • Bridge Method--is the compiler's tool for maintaining polymorphism
      • There are no generics in the virtual machine, there are only ordinary classes and methods
      • To preserve type safety, insert a forced type conversion if necessary
    4. Calling Legacy code
  5. Constraints and limitations
    1. Cannot instantiate with a basic data type
      • Pair (Double) √pair (double) x
    2. Run-time type checking applies only to raw data types
      • A instanceof pair<t> x
      • A instanceof pair<string> x
      • Pair<string> a = (pair<string) b x
      • The GetClass method returns always the original data type
    3. Cannot create an array of parameterized types
      • pair<string> table = new PAIR<STRING>[10] X
      • There is only one way to arraylist<pair<string>>
    4. Cannot instantiate type variable
      • To have a constructor expression provided by the caller
      • Pair<string> p = Pair.makepair (string::new)
      • Makepair (supplier<t> constr)
      • return new Pair<> (Constr.get (), Constr.get ())
    5. Cannot create a generic array
      • Cannot be done, however.
      • Public static<t extends comparable> t[] Minmax (intfuction<t[] constr, T ... a)
      • When called, it is string[] ss = Arrayalg.minmax (String[]::new, "Tom", "Jim", "Harry").
    6. Invalid type variable in the static context of a generic class
      • That is, if something in a generic class is modified by static, it cannot have t
    7. Cannot throw or capture generic class instances
      • Generic class extension Throwable is not legal
      • Unable to capture generic Data sample T T is not captured
      • Public static<t extends throwable> void DoWork (class<t> T)
      • catch (T E) x
      • The use of type variables is legal
      • Public static<t extends throwable> void doWork (T t) throws T
      • catch (Throwabe realcause) √
    8. The conflict after wiping out
      • To support the erase conversion, you need to force the restriction that a class or type variable cannot be a subclass of two interface types at the same time, and these two interfaces are different parameterization of the same interface!
      • Class Employee implements Comparable<employee>
      • Class Manager extends Employee implements Comparable<manager>
      • At this point, the Manager will implement comparable<employee> and comparable<manager>
  6. Inheritance rules for generic types
    1. Pair<employee> and pair<manager> don't matter.
  7. Wild-Letter wildcard type
    1. Concept: <? Extends employee> means any pair type
      • Pair<manager> is pair< Extends subtypes of employee>
    2. Super-type qualification of wildcard characters
      • <? Super xx>
    3. Unqualified wildcard characters
      • Pair<?> the Set method cannot be called

Simple Java Generics summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.