Java Generic Notes

Source: Internet
Author: User

The purpose of Java generics:
versatility, which can be applied to multiple types rather than to a specific interface or class.
JAVA1.5 's supplement.
used to specify what type of object the container is to hold, and the compiler guarantees the correctness of the type.

Core concepts of Java generics:
tell the compiler what type you want to use, and the compiler can help you with all the details.

Limitations of Java generics :
1, you cannot use the base type as a type parameter.

-java Generic methods :
You can use generic methods to use generic methods as much as possible.
static method, cannot access the type parameter of the generic class.

define a generic method: Define a generic parameter list before the return value
Public static <T> void Test () {
 
  }
(when you use a generic class, you must specify the value of the type parameter when you create the object, and when you use a generic method, you typically do not have to indicate the type of the parameter, because the compiler finds the specific type for you.) This is called type parameter inference. )
when specifically implemented, the compiler cannot infer the type as
list<person> List = new arraylist<person> ();

call the generic method, display the type description, insert the type between the. operator and the method name
new Generator (). <suborder>test (new Suborder ());

Java Generic Notes

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.