Type erasure for Java generic generic classes

Source: Internet
Author: User

Any one of the generic types corresponds to this one primitive type. The name of the original type is derived from the result of removing the parameter from the generic type name with the parameter, and substituting the type variable in the class with the qualified type of the type variable (object if there is no qualified type). Here is an example from the Java Core Technology Volume 1:

Before type erase:

Package generic;/** * @version 1.00 2004-05-10 * @author Cay Horstmann */public class Pair<t> {   private T first;   private T second;   Public Pair () {first = null; second = null;}   Public Pair (t first, T second) {this.first = first;  This.second = second; } public   T GetFirst () {return first;}   Public T Getsecond () {return second;}   public void Setfirst (T newvalue) {first = newvalue;}   public void Setsecond (T newvalue) {second = newvalue;}}

After type erase:

Package generic;/** * pair<t> type erased version, that is, the original type of pair<t>; * Erase rule is to delete the type variable following the class name and replace the class with the * type variable with the qualified type of the type variable (* If there is no qualifying type, use object) * @author Yuncong * */public class Pair {   private object first;   Private Object second;   Public Pairorigin () {first = null; second = null;}   Public Pairorigin (object first, object second) {This.first = first;  This.second = second; } public   Object GetFirst () {return first;}   Public Object Getsecond () {return second;}   public void Setfirst (Object newvalue) {first = newvalue;}   public void Setsecond (Object newvalue) {second = newvalue;}}
For tests of generic classes, there are two helper classes:

Package Generic;public class Person extends Animal {private string name;public person (String name) {super (); this.name = NA Me;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} @Overridepublic String toString () {return "person [name=" + name + "]";}}

Package Generic;public class Student extends person {private String studentnumber;public Student (string name, String regis Trationnumber) {super (name); this.studentnumber = Registrationnumber;} Public String Getstudentnumber () {return studentnumber;} public void Setstudentnumber (String studentnumber) {this.studentnumber = Studentnumber;} @Overridepublic String toString () {return "Student [studentnumber=" + Studentnumber + ", name=" + getName () + "]";}}


The following test illustrates the actual procedure for invoking the GetFirst () method of pair<t>:

Package Generic;public class Test3 {public static void main (string[] args) {pair<person> Pair = new PAIR<PERSON&G t; (New person ("Li"), New person ("Wang"));/** * Because pair<t> after the type erase, its * GetFirst () method return type is actually object, * so here actually went through two steps: * 1. Call to the original method Pair.getfirst () * 2. Force the returned object type to be converted to person type */person person = Pair.getfirst ();/** * For the above test, the following knowledge is necessary to understand */// Assigning a parent type to a subtype requires forcing the type to convert person Person2 = new Person ("Yuncong"); Person Person3 = new Student ("Yuncong", "1"); Student Student = (Student) person3;//ok//if the actual type of the parent type is not a subtype, the styling error is reported Student student1 = (Student) person2;//Error}}

The following test shows that the parameterized type is a subtype of its original type:

Package Generic;public class Test4 {public static void main (string[] args) {pair<person> Personpair = new Pair<pe Rson> ();//The parameterized type can be converted to its original type pair pair = Personpair; Building Building = new Building ("Dingxin");/** * OK, Building can be passed to pair; * Additional security (type checking) provided by generic programming is lost */pair.setfirst ( Building); Person person = (person) pair.getfirst (); Styling Error/** * The method Setfirst (person) in the type pair<person> * are not applicable for the  arguments (buildin g) *///error, cannot pass building to Personpair, generic programming provides type safety Personpair.setfirst (building);}}


Type erasure for Java generic generic classes

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.