Generic, java generic

Source: Internet
Author: User

Generic, java generic
1. Concept Analysis

First, you need to know that the wildcard here isParameter typeTo some extent, the range of parameter types is extended. The word "type" can be understood as a strong type support, that is, the parameter type must belong to a certain type, and some restrictions may be imposed on this type, that is, the so-called "generic parameter constraints ". Both "strong type support" and "generic parameter constraints" limit the range of parameter types to some extent, reflecting the idea of "refinement.


2. Functions of generics

The "generic" mentioned above reflects the abstract idea, that is, the parameter type is abstracted at a high-level level, so that the parameter type is converted from dependent on specific to dependent on abstract, so thatCode can be reused,More programsFlexible. The word "type" indicates that a strong type supports + generic parameter constraints, that is, it specifies that the parameter type must be of a certain type, and some constraints may be attached to this type. "Strong type support" avoids implicit packing and unpacking, thusImproves Program PerformanceStrong type support + generic parameter constraints determine that during compilation, you can find a place that does not meet the parameter type requirements. In this wayThis ensures the type security during compilation,ThusAvoid type conversion errors during running.. To sum up, "generic" is used for code reuse and more flexible programs. "type" is used to improve program performance and security.


3. When do I need generics? How to use it?

As mentioned above, there are two main functions of generics: [role 1] code can be reused through abstract parameter types, and programs are more flexible; [role 2] enhanced program performance and security through strong type support and generic parameter constraints. In this way, we can understand when generic type is needed:


3.1 When the parameter type is not fixed and is easy to change, you can solve the problem of generic [function 1] when you want to make the program respond flexibly. For example:
  

As shown in, the Bubble Sorting Algorithm is written in two ways: "Do not use generics" and "use generics. The former can only sort the int array type, and the latter can sort the array that inherits the IComparable interface type (such as char and int, as shown in.

The yellow highlight is the place where the generics are never used or changed. You can see four changes in total:

Ø first, <T> is added after the method name, indicating that the parameter unit must be of the T type, that is, "strongly supported ".

Then, the parameter type is changed from a fixed int [] to an uncertain T [], that is, the so-called "parameter type abstraction ".

Then, whereT: IComparable is added to the method, which indicates that the IComparable interface must be implemented for the T type, that is, the so-called "generic parameter constraint ".

Then, the comparison method is changed from "<" to the CompareTo (object) method. This method exists in the IComparable interface and is the common feature extracted from char, int, and other types, because they all implement this interface.

For example, int and char Types all implement the IComparable interface:


  

  


  Instance summary:A non-generic algorithm cannot apply to multiple types. The root cause is that the algorithm uses a specific type, that is: '<' in array [j] <array [J-1. To apply multiple types of objects, you only need to find the comparison methods applicable to these types of objects, just as C # has a common comparison method, that is, CompareTo (object ), this method is located in the IComparable interface, so you only need to implement this interface to enjoy its CompareTo (object) method. So we abstracted the various types to get the type T (uncertain type), and then let T implement the IComparable interface, isn't it equivalent to implementing the interface for multiple types, can I use the CompareTo (object) method of the interface even if this interface is implemented? Can this problem be solved.

The solution for this instance is to find the change point and the same point, and then replace the change point with the same point. The main method is "abstract parameter type", and of course it also includes "strong type support" and "generic parameter constraints ".


3.2 When you want to improve program performance and security, generic [role 2] may solve

Because "strong type support" avoids implicit packing and unpacking, thus improving program performance. The following example:

  

Strong type support + generic parameter constraints determine that during compilation, you can find a place that does not meet the parameter type requirements. This ensures the type security during compilation, this avoids type conversion errors during running. Example:

  


 

4. generic classes and generic methods

The two are only supported in the declaration of the forced type and the position of the generic parameter constraints is different, one after the class and the other after the method. The only difference is that the scope is different. Example:

  


5. generics and collections

The reason why generics are often used together with collections is that a set contains multiple or even many objects. Once a collection involves Yin packing and unpacking operations, this process is required for all objects in the entire set, which greatly reduces program performance. On the contrary, if the generic type is used, the overcast disassembly box will be avoided, and of course the program performance will be greatly improved. Of course, it can also ensure the type security during compilation and avoid type conversion errors during runtime.

Therefore, the set operation mainly applies "strong type support" in generic [role 2 ".


6. Summary

This section describes the functions of generics from the literal and application examples:

Through the abstract parameter typeCode can be reused, and the program is more flexible;

The "strong type support" of the parameter avoids implicit packing and unpacking, thusImproves Program Performance;

Through strong type support + generic parameter constraints, it is determined that the location that does not meet the parameter type requirements can be found during compilation.This ensures the type security during compilation and avoids type conversion errors during runtime..


What is generic in java ??

What is generic
Generic is a feature of programming languages. Allow programmers to write and experience generics in strongly typed programming languages
Some variable parts are defined in the Code, which must be specified before use. Different programming languages have different support for generics from their compilers and runtime environments. A data type that parameterizes types to achieve code reuse and improve the efficiency of software development. A generic class is a reference type and a heap object. It mainly introduces the concept of a type parameter.
Generic definition
There are two types of generic definitions:
In program code, some types contain type parameters. That is to say, generic parameters can only represent classes and cannot represent individual objects. (This is a common definition today) Some Classes Containing Parameters in program encoding. Its parameters can represent classes or objects. (Most people now call this a template) regardless of the definition, generic parameters must be specified when they actually use generics.
Some strong programming languages Support generics. The main purpose is to enhance type security and reduce the number of class conversions, but some programming languages that support generics can only achieve some purpose.

What is generic in java ??

What is generic
Generic is a feature of programming languages. Allow programmers to write and experience generics in strongly typed programming languages
Some variable parts are defined in the Code, which must be specified before use. Different programming languages have different support for generics from their compilers and runtime environments. A data type that parameterizes types to achieve code reuse and improve the efficiency of software development. A generic class is a reference type and a heap object. It mainly introduces the concept of a type parameter.
Generic definition
There are two types of generic definitions:
In program code, some types contain type parameters. That is to say, generic parameters can only represent classes and cannot represent individual objects. (This is a common definition today) Some Classes Containing Parameters in program encoding. Its parameters can represent classes or objects. (Most people now call this a template) regardless of the definition, generic parameters must be specified when they actually use generics.
Some strong programming languages Support generics. The main purpose is to enhance type security and reduce the number of class conversions, but some programming languages that support generics can only achieve some purpose.

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.