Java Generics re-learning

Source: Internet
Author: User

Generics are a specification of data, and he restricts the types of arguments that classes, containers, and methods can accept to avoid confusing parameter types.

First, the basic generic type

The most common areas of generics are collections, such as:

--arraylist<string> indicates that the collection can only hold elements of type String

--Hashmap<string, object> indicates that this diagram can only hold elements of type String and value Object.

It is especially important to note that generics exist only in the compile phase, and in the program runtime, we define generics that do not exist, which is called "erase", example:

1  Public classGenerictest {2 3      Public Static voidMain (string[] args) {4Arraylist<string> A =NewArraylist<string>();5Arraylist<integer> B =NewArraylist<integer>();6System.out.println (a.getclass () = =B.getclass ()); True7     }8     9}

As can be seen from the example, although two different ArrayList collections of generic types are defined, they belong to a single byte code, which proves that the survival phase of generics exists only at compile time.

Ii. generic methods

The concept of a Java generic method is based on a C + + template function, but because of a problem with the JVM instruction set, Java does not fully implement a generic method that is exactly the same as a C + + template method, and the generic method is defined as follows:

Access modifiers define generic type return value type method name (parameter list ...) {

//....

}

1  Public class generictest {2     3      public <T> t Add (t I, T j) {4         returnnull; 5     }67 }

To illustrate, the definition of a generic type does not necessarily use T, but the industry is accustomed to using T, such as ARRAYLIST<E> A generic type represents an arbitrary reference type .

As the above example, we define a generic method that uses <T> to define the generic type to be used by this method before the return value T, and then pass in two variables of type T, for which you can execute the following call:

--Add (+);

--Add (3.5,12);

--Add ("abc", True);

--Add ("xyz");

You can see that except for the first call, the two arguments passed in are the same type, and the other calls pass in different types of arguments. But the method can be executed correctly, which is type deduction. Simply put, the type of the result of an operation is always the largest of the operands in the two different types of data. That is, the int and float operations, the result is a float type, a string and a Boolean operation, the result is a string type ...

Third, generic type

Generic classes are more commonly used in development, and the following is a pagemodel of the paging tool class to be used in the actual development:

1  Public classPagemodel<t> {2 3     PrivateInteger PageNo;4     5     PrivateInteger pageSize;6     7     PrivateInteger totalrecords;8     9     PrivateList<t>Records;Ten  One     //Omit Getter&setter Method A}

When you qualify a generic type after the class name, you can use that type to qualify the variable and method in the entire class.

Perform the following initialization:

1 New Pagemodel<user> ();

At this point, the property records represents a collection in which only the user object is stored, which is a use of the generic class. To put it bluntly, generic classes, generic types defined after the class name are not useful to the class itself, but are used only to qualify class members.

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.