java-generic "excerpt"

Source: Internet
Author: User

Generic, which is the parameterized type. When referring to a parameter, it is most familiar to define the method when the physical parameter is called and then pass the argument when calling this method. So how do parameterized types understand? As the name implies, the type is parameterized by the original specific type, similar to the variable parameter in the method, when the type is also defined as a Parameter form (which can be called a type parameter), and then the specific type (type argument) is passed in when using/calling.

Custom generic interfaces, generic classes, and generic methods

 Public classGenerictest { Public Static voidMain (string[] args) {Box<String> name =NewBox<string> ("Corn"); System.out.println ("Name:" +name.getdata ()); }}classBox<t> {    PrivateT data;  PublicBox () {} PublicBox (T data) { This. data =data; }     PublicT GetData () {returndata; }}

As a result, we find that when using a generic class, although different generic arguments are passed in, but there is no real sense of generating different types, the generic classes passing in different generic arguments have only one in memory, that is, the original basic type (box in this case), of course, Logically, we can understand a number of different generic types.

The reason for this is that the concept of generics in Java is intended to be a function of the code compilation phase, during the compilation process, for the correct validation of generic results, the generic information will be erased, that is, the successful compilation of the class file does not contain any generic information. Generic information is not entered into the run-time phase.

This is summed up in a sentence: a generic type is logically viewed as multiple different types, and is actually the same basic type.

Windstep

java-generic "excerpt"

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.