Java generic exploration-small features, java generic features

Source: Internet
Author: User

Java generic exploration-small features, java generic features
Generic features (small space)

1. Introduce some common generic features:

The type parameter T can be a recursive (similar to a progressive type), and its boundary can be its own interface or class.

For example, if I find the maximum value, I can write it like this:

public static <T extends Comparable<T>> T max(Collection<T> coll) {    T candidate = coll.iterator().next();    for (T elt : coll) {        if(candidate.compareTo(elt) < 0)            candidate = elt;    }    return candidate;}

Multiple Bounds)

    public static<S extends Readable & Closeable, T extends Appendable & Closeable>     void copy(S src ,T trg, int size) throws IOException {        try {         CharBuffer buf = CharBuffer.allocate(size);         int i = src.read(buf);         while (i >= 0) {             buf.flip();             trg.append(buf);             buf.clear();             i = src.read(buf);         }        } finally {            src.close();            trg.close();        }    }

 

2. Bridges features

For generic interfaces, such as Comparable <T>, additional methods are inserted during compilation. These methods are called bridges.

How can the Comparable interface be implemented before the generic type exists? Some code is omitted. The Code is as follows:

interface Comparable {    public int compareTo(T o); }class Integer implements Comparable {     private final int value;    public Integer(int value) {         this.value = value;     }     public int compareTo(Integer i) {        return (value < i.value) ? -1 : (value == i.value) ? 0 : 1;     }    public int compareTo(Object o) {        return this.compareTo((Integer)o);    }}

We call the compareTo (Object o) method bridge.

The bridge method is generated during jvm compilation for generic interfaces. We use Comparable <T> as an example:

interface Comparable<T> {    public int compareTo(T o); }class Integer implements Comparable<Integer> {     private final int value;    public Integer(int value) {         this.value = value;     }     public int compareTo(Integer i) {        return (value < i.value) ? -1 : (value == i.value) ? 0 : 1;     }
}

Taking Integer as an example, we implement the Comparable interface. through reflection, we can see several compareTo methods. see:

for (Method m : Integer.class.getMethods()) {     if (m.getName().equals("compareTo"))     System.out.println(m.toGenericString());}

The output result is as follows:

public int java.lang.Integer.compareTo(java.lang.Integer)public int java.lang.Integer.compareTo(java.lang.Object)

It can be seen that this is indeed the case. As for why, let's take a look at it. Let's leave a hole first. Let's take a look at the usefulness of the bridge method.

 

3. Covariant Overriding (Covariant coverage)

In java1.4, the override of a method must be strictly the same as that of the return type. After java1.5, The overwrite of a method only requires the same list of parameters, the parameter list is the same, if the return type

Is the subclass of the parent class. The method of this subclass can also overriding the method of the parent class.

Let's take the clone method as an example. In the Object class:

class Object {    ...    public Object clone() { ... }}

In the Point class:

class Point extends Object {    public int x;    public int y;    public Point(int x,int y) {        this.x = x;        this.y = y;    }    public Point clone() {        return new Point(x,y);    }}

Before java1.4, the above clone method could not cover the Object's clone method, but it could be written from java1.5. Why? Or bridge's merits:

Let's take a look at reflection. There are several clone methods in the Point class:

for(Method m : Point.class.getMethods()) {   if(m.getName().equals("clone"))System.out.println(m.toGenericString());}
The output is as follows:
Output
Public Point. clone () public java. lang. Object Point. clone () throws java. lang. CloneNotSupportedException
The java. lang. Object Point. clone () method simply calls the Point. clone () method and overwrites it.

Well, these are scattered generic knowledge points. Let's introduce them first and continue todo later.

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.