Java programming Java generic method Java development Learning

Source: Internet
Author: User

java generic methods and generic classes enable programmers to use a method to specify a set of related methods, or to use a class to specify a set of related types.

Java generics are a new feature introduced in JDK 5 , and generics provide a compile-time type-safe detection mechanism that allows programmers to detect illegal types at compile time.

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.

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:

1 . All generic method declarations have a part of a type parameter declaration that is part of the type parameter declaration before the method return type.

2, each type parameter declaration part contains one or more type parameters, the parameters are separated by commas. A generic parameter, also known as a type variable, is the identifier used to specify a generic type name.

3, the type parameter can be used to declare the return value type, and can be used as a generic method to get the actual parameter type placeholder.

4 . The declaration of the generic method method body is the same as other methods. Note the type parameter can only represent a reference type and cannot be the original type.

Generic class:

As with generic methods, the type parameter declaration portion of a generic class also contains one or more type parameters, separated by commas. A generic parameter is an identifier that specifies the name of a generic type.

If you want to stand out from the many IT workers, you need to have advanced technology and learning to add value is essential. The way of learning, is your insistence!

Java programming Java generic method Java development Learning

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.