Java-based Java generics

Source: Internet
Author: User

Java generics (generics) is a new feature introduced in JDK 5, which provides a compile-time type-safe detection mechanism that allows programmers to detect illegal types at compile time. The essence of a generic is a parameterized type, which means that the data type being manipulated is specified as a parameter. Suppose we have a need to write a sorting method that can sort an array of integers, arrays of strings, and even any other type of array, how do we do that? The answer is that you can use Java generics. Using the concept of Java generics, we can write a generic method to sort an array of objects. The generic method is then called to sort an integer array, a floating-point array, an array of strings, and so on.



650) this.width=650; "src=" http://upload.lanqiao.cn/image/20170711/1499737190230371.jpg "title=" 1499737190230371. JPG "alt=" u=3645943253,1699441531&fm=26&gp=0.jpg "style=" margin:0px;padding:0px;border:0px; Vertical-align:middle; "/>




Generic methods



You can write a generic method that can receive different types of arguments when it is called. Depending on the type of argument passed to the generic method, the compiler handles each method call appropriately.


The following is the rule that defines a generic method:



All generic method declarations have one type parameter declaration part (delimited by angle brackets), which is part of the type parameter declaration before the method return type


each type parameter declaration section contains one or more type parameters, separated by commas between the parameters. A generic parameter, also known as a type variable, is the identifier used to specify a generic type name. a type parameter can be used to declare a placeholder for the actual parameter type that returns a value type and can be obtained as a generic method. the declaration of a generic method body is the same as other methods. Note the type parameter can only represent the reference type, not the original type (Int,double,char, and so on).


Java-based Java generics

Related Article

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.