Summary of knowledge points in the process of Java review

Source: Internet
Author: User

                Summary of knowledge points in the process of Java review

About generics

 Why use generics?

A collection can store arbitrary objects, but if you need to use an object-specific method, type conversions are required, and a type conversion exception occurs if the collection is stored in a different object. And it is not possible to find problems during compile time. Type checking (instanceof) can be done with the IF statement at the time of type conversion, but the efficiency is not high. Therefore, the concept of generics is introduced.

Advantages of using generics:

1. After using generics, the object being removed does not require a forced type conversion

2. Advance the runtime exception to the compile time

When do I use generics?

When the reference data type of the operation in the class is not deterministic.

Issues to be aware of using generics:

1. When a generic is declared, the collection can only hold specific types of elements

2. The generic type must be a reference type, and a basic type is required, using the wrapper class

3. After using generics, the removed element does not require a type conversion

Generic methods

generics : The type is treated as a variable. The definition of a canonical generic is usually an uppercase letter of any type

a generic definition on a function:

When a function uses an ambiguous data type, a generic definition can be made on the function

public < generic declaration > Return value type function name (generic variable name)

Using the generic method details:

Before using a generic method, you need to make a generic declaration of,<, where the declared position is preceded by the return value type after static

When a class has multiple functions that declare generics, a function declaration can be declared on a class

Generic class

format : Modifier class class name < generic >{}

Note : Static methods cannot use generic types defined by the class

Generics in a class need to specify a specific type when the object is initialized, while static is better than the object exists. Static methods in that class require a separate generic declaration

details of the generic class :

1. When creating an object, specify the type of the generic and, if not specified, the default object type

2. After the object is created, the specific type of the generic is specified, and when the object is manipulated, the methods can only manipulate one data type

If a generic class specifies a subclass:

1. Subclasses can specify a specific type, or you can use a generic

Class Son<t> extends father<t>{}

Generic interface

Implementations do not know what type is defined:

Class Myinter<t> implements Inter<t>

Explicitly specific types when using interfaces

Class MyInter2 implements Inter<string>

Summary of knowledge points in the process of Java review

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.