Analysis and summary of generic knowledge points in Java __java

Source: Internet
Author: User
Tags comparable
first, the generic foundation (1) generic type

Let's start by defining a simple box class:

public class Box {    
private String object;   
public void Set (String object) { 
    This.object = object; 
    }    
Public String get () {return 
    object; 
    }
}

This is the most common practice, one disadvantage of this is that box can now only load string elements, in the future if we need to load the integer and other types of elements, but also have to rewrite a box, the code is not reused, using generics can be a good solution to this problem.

public class Box<t> {    //T stands for "Type"
    private t T;   
    public void Set (T-t) {
    this.t = t;
  }    
  Public T get () { 
    }
}

So that our box class can be reused, we can replace T with any type we want:

box<integer> Integerbox = new box<integer> ();
box<double> Doublebox = new box<double> ();
box<string> Stringbox = new box<string> ();
(2) generic method

After reading the generic class, let's take a look at the generic approach. Declaring a generic method is simple, just precede the return type with a similar
The "< K,v >" form is on the line:

public class Util {public    
static <k, v> Boolean compare (Pair<k, v> p1, pair<k, v> p2) {       
 return P1.getkey (). Equals (P2.getkey ()) &&p1.getvalue (). Equals (P2.getvalue ());
    }
public class Pair<k, v> {    
private K key;    
private V value;    
Public Pair (K key, V value) {        
This.key = key;        
This.value = value;
    }    
public void Setkey (K key) { 
This.key = key; 
}    
public void SetValue (V value) { 
this.value = value; 
}    
Public K Getkey ()   {return 
key; 
}   
 Public v. GetValue () {return 
 value; 
 }
}

We can invoke the generic method as follows:

Pair<integer, string> p1 = new pair<> (1, "Apple");
Pair<integer, string> p2 = new Pair<> (2, "pear");
Boolean same = Util.<integer, String>compare (P1, p2);
Type Erase 1, what is type erase:

Type erasure means that a Java generic can only be used for static type checking during compilation, and then the compiler-generated code erases the corresponding type information, so that in fact the JVM knows exactly what type the generics represent at runtime. The reason for this is that the Java generics were introduced after 1.5, and in order to maintain downward compatibility, only type erasure can be done to be compatible with the previous Non-generic code. For this, if you read the source code of the Java Collection framework, you can see that some classes do not actually support generics.

Having said so much, what does the generic erase mean? Let's take a look at the following simple example:

public class Node<t> {   
private T data;    
Public Node (T data, node<t> next) {   
this.data = data;        
This.next = next;   
 Public T GetData () {return 
 data;
}    
// ...}

After the compiler has done the corresponding type check, actually the code above the runtime will actually be converted to:

public class Node {    
private Object data;    
Private Node Next;   
Public Node (Object data, Node next) { 
this.data = data;        
This.next = next;    
Public Object GetData () {return 
 data; 
 }    
 // ...
 }

This means that regardless of whether we declare node or node, the JVM is treated as node at runtime. Is there any way to solve the problem? This requires that we reset the bounds ourselves, and change the above code to the following:

public class Node<t extends comparable<t>> {    
private T data;    
Private node<t> Next;    
Public Node (T data, node<t> next) {        
this.data = data;        
This.next = next;    
Public T GetData () {return
 data; 
 }    // ...
 }

This allows the compiler to replace the place where T appears as comparable instead of the default object:

public class Node {    
private comparable data;   
Private Node Next;    
Public Node (comparable data, Node next) {        
this.data = data;        
This.next = next;    
Public comparable GetData () {return 
data; 
}    // ...
}

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.