java-Basics-Generics

Source: Internet
Author: User

When you use a collection, you must explicitly specify the type of the elements in the collection. This approach is called: Generics.

Definition Format: modifier class class name < variable representing generic type > {}

To determine the type of a generic when creating an object

For example,arraylist<string> list = new arraylist<string> ();

At this point, the value of the variable e is the string type

class Arraylist<string>{publicboolean  Add (String e) {}    public String get (int  index) {}}

For example,arraylist<integer> list = new arraylist<integer> ();

At this point, the value of the variable e is the integer type

class Arraylist<integer>{publicboolean  Add (Integer e) {}    public Integer get (int  index) {}}
methods that contain generics

Definition format: Modifier < variable representing generic type > Return value type method name (parameter) {}

For example, the methods in the ArrayList collection in the API:

Public <T> T[] toArray(t[] a) {}

The method used to store the collection element in an array of the specified data type, returning an array of stored collection elements

Use format: When calling a method, determine the type of the generic type

For example

arraylist<string> list = new arraylist<string> ();

string[] arr = new string[100];

String[] result = List.toarray (arr);

At this point, the value of the variable t is a string type. Variable T, which can differ from the generics that define the collection

Public <String> String[] toArray(string[] a) {

For example

arraylist<string> list = new arraylist<string> ();

integer[] arr = new integer[100];

Integer [] result = List.toarray (arr);

At this point, the value of the variable t is the integer type. Variable T, which can differ from the generics that define the collection

Public <Integer> integer[] toArray(integer[] a) {}

interfaces that contain generics

Definition format: Modifier interface interface name < variable representing generic type > {}

For example, the iterator iterator interface in the API

Public interface Iterator<e> {

Public abstract E Next ();

}

Use format:

To determine the type of a generic when defining a class

For example

Public final class Scanner implements Iterator<String> {

Public String next () {}

}

At this point, the value of the variable e is the string type.

The type of the generic is always indeterminate until the type of the generic is determined when the object is created

For example

arraylist<string> list = new arraylist<string> ();

Iterator<string> it = List.iterator ();

At this point, the value of the variable e is the string type.

public interface iterator<String> {

Public abstract String next ();

}

benefits of using generics

The classcastexception of the runtime was transferred to the compile-time and failed to compile.

Avoid the trouble of type strong turn.

 Public classGenericdemo { Public Static voidMain (string[] args) {List<String> list =NewArraylist<string>(); List.add ("ABC"); List.add ("Itcast"); //List.add (5);//when the set type is explicit, the storage type is inconsistent and the error is compiled.//The collection already explicitly stores the element type, so when using an iterator, the iterator will also know the specific TRAVERSAL element typeIterator<string> it =List.iterator ();  while(It.hasnext ()) {String str=It.next ();//when you use Iterator<string> to control element types, you do not need a strong turn. Gets the element that is directly the string typeSystem.out.println (Str.length ()); }    }}
generic wild-wildcard characters

Generics are in the qualified data type, when used in the collection or elsewhere, when the generic type, then once the generic data type, then when used only to pass to the type and data type matching, otherwise it will be an error.

When using a generic class or interface, the generic type is not deterministic in the data passed, and can be represented by a wildcard character <?>. But once a generic wildcard is used,

Only the common methods in the object class can be used, and the elements ' own methods in the collection cannot be used.

Generic Qualification

Limit the limits of generics:

Format:? Extends E

? Represents an element that receives a subtype of type E or E

For example, a generic qualification is:? Extends person

The? Represents the element that receives the person type or person subtype

Limiting the lower limit of generics:

Format:? Super E

? Represents the element that receives the parent type of type E or E

For example, a generic qualification is:? Super Student

The? Represents an element that receives a student type or student a parent type

java-Basics-Generics

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.