201671010140.2016-2017-2 Java Program Tenth week of Java learning

Source: Internet
Author: User

---restore content starts---

Java Learning Week Tenth

this week, learn generic programming, generics are also known as parameterized types (parameterized type), that is, when defining classes, interfaces, and methods, the type parameter indicates the type of object that will be processed (such as the ArrayList Class). Generic program setting (genericprogramming) means that writing code can be reused by many different types of objects. It is important to note that the generic attribute is forward-compatible.

the greatest benefits of generics are considered by individuals: eliminating the forced type conversion makes the code more readable and reduces the chances of error; the benefits of introducing generics into the Java language are simple and secure. The benefit of generics is that it checks for type safety at compile time, and all casts are automatic and implicit, increasing the rate of reuse of the code.

generic class definition: Add <> after the class name, and then add the type parameter, such as Class pair<t>, a generic class can have more than one type variable. For example: public class pair<t, u> {...}. (type variables are capitalized). Sometimes, classes, interfaces, or methods need to constrain type variables, such as examples Pair1 and pair2 in books, with the qualification of generic variables:

1, whether the qualification is a class or interface, unified use the keyword extends

2, can use the & symbol to give a number of limits

3, if there is a limit to the existing interface and class, then the class must be only one, and placed in the first place

After practicing the annotation of the sample program, I helped me understand the knowledge of the generic class better, but the knowledge of the inheritance rules of the generic type is a little vague, it will seek help in the book and blog Park, and other tools can help us to solve the problem of learning.

---restore content ends---

201671010140.2016-2017-2 Java Program Tenth week of Java 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.