Java generic 2 (j2se entry 24)

Source: Internet
Author: User

Generic method definition
When copying an array to a set, the array type must be the same as the generic type of the set.
<...> Define the generic type, where "... "It is generally replaced by an uppercase letter, that is, the generic name. In fact, the generic type will be replaced at runtime according to the actual type.

<E> void copyArrayToList(E[] os,List<E> lst){
for(E o:os){
lst.add(o);
}
}
static <E extends Number> void copyArrayToList(E[] os,List<E> lst){
for(E o:os){
lst.add(o);
}
}
static<E extends Number & Comparable> void copyArrayToList(E[] os,List<E> lst){
for(E o:os){
lst.add(o);
}
}



Restricted generic type indicates that the value range of the type parameter is limited. the extends keyword can be used not only to declare the inheritance relationship of a class, but also to declare the restricted relationship of a type parameter. for example, we only need a list of numbers, including integers (long, integer, short) and real numbers (double, float). It cannot be used to store other types, such as strings ), that is to say, we need to restrict the value of type parameter T to the number extremely subclass. in this case, we can use the extends keyword to limit the type parameter to a number.
You can only use extends, not super, but downward, not up.
Used for calling <?> <E>

Definition of generic classes

1> do not use generics for static methods and static attributes of classes, because generic classes are generated when an object is created.

class MyClass<E>{
public void show(E a){
System.out.println(a);
}
public E get(){
return null;
}
}


Restricted Generic

class MyClass <E extends Number>{
public void show(E a){
}
}


Generics and exceptions

Type parameters are not allowed in catch blocks, but can be used after the throws of the method. Example:

import java.io.*;

interface Executor<E extends Exception> {
void execute() throws E;
}

public class GenericExceptionTest {
public static void main(String args[]) {
try {
Executor<IOException> e = new Executor<IOException>() {
public void execute() throws IOException{
// code here that may throw an
// IOException or a subtype of
// IOException
}
}
e.execute();
} catch(IOException ioe) {
System.out.println("IOException: " + ioe);
ioe.printStackTrace();
}
}
}


Limitations of generic
1. Catch cannot use generics. In a generic set, you cannot use generics to create objects. You cannot use generic objects.
2. The generic type cannot be instantiated.
3. arrays of the generic type cannot be instantiated.
4. The number of generic parameters cannot be instantiated.
5. Static variables of the class cannot be declared as type parameters.

Public class genclass <t> {
Private Static t; // compilation Error
}


6. Static methods can be generic methods, but they cannot be generic.
7. generic classes cannot inherit from throwable and its subclass.
Public genexpection <t> extends exception {} // compilation Error
8, cannot be used for basic types such as int

Pair <double> // Error
Pair <double> // right

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.