Java-related Help---Understanding and equivalence implementation of Java generics---------not fully understand Ah ... Ask the Great God to guide ... __java

Source: Internet
Author: User
Tags java se

1. Generics are a new feature of Java 1.5, the essence of which is a parameterized type, which means that the data type being manipulated is specified as a parameter. This type of parameter can be used in the creation of classes, interfaces, and methods, called generic classes, generic interfaces, and generic methods, respectively.

The benefits of 2.Java generics being introduced are safe and simple.

Before the Java SE 1.5, without generics, the argument is "arbitrary" by reference to type object, and the disadvantage of "arbitrariness" is to do explicit coercion of type conversions, which require the developer to be able to predict the actual parameter types. In the case of coercion type conversion errors, the compiler may not prompt for an error and the exception occurs at run time, which is a security risk.

The benefit of generics is to check type safety at compile time, and all casts are automatic and implicit, increasing the reuse rate of code.

Generics also have some rules and restrictions in use:

1. A generic type parameter can only be a class type (including a custom class) and cannot be a simple type.

2. The same generic type can correspond to multiple versions (because the parameter types are indeterminate) and different versions of generic class instances are incompatible.

3, the generic type parameter can have more than one.

4, the generic type of parameter can use the extends statement, for example. Become accustomed to being "bounded type".

5. The parameter type of the generic type can also be a wildcard character type. For example Class ClassType = Class.forName (java.lang.String);

Generics also have interfaces, methods, and much more, and it takes a lot of effort to understand mastery and proficiency in application. Here are two examples of what I've learned about generics (based on impressions), the same functionality, one that uses generics, one that's not used, and by contrast, you can quickly learn generic applications and learn to basically learn the generic 70% content.

Example one: Java generics are used

------------------------------------------------------------------------------------------------

Here I used the javase1.6, but still made a mistake ... Below the specific error, if you correct the error, then can not play the role of generics, or need to force type conversion so that there is a security problem

------------------------------------------------------------------------------------------------------

public class gen { //key here, or to add gen<t> private t ob; //define generic member variables     Public gen (t ob)  {  this.ob = ob;  }     Public t&nbs P;getob ()  {  return ob;  }     Public void setob (t ob)  {  thi s.ob = ob;  }     Public void showtyep ()  {  System.out.println ("T's actual type is: "  +  ob.getclass (). GetName ());  }  }     public class gendemo {  Public static void main ( String[] args) { //define an integer version of the generic class Gen   Gen intob=new gen;   Intob.showtyep ();  nbsp Int i= intob.getob ()///Here you still need to force type conversion, no conversion will be error   SYSTEM.OUT.PRINTLN ("value= "  + i);     SYSTEM.OUT.PRINTLN ("----------------------------------");    //define a string version of the generic class Gen   Gen strob=new gen ("hello gen!");   Strob.showtyep ();   String s=strob.getob ()///Here you still need to force type conversion, do not convert will be error   SYSTEM.OUT.PRINTLN ("value= "  + s);  }  } 

Example two: not using generics

public class Gen2 {private Object ob;//define a Universal type member public Gen2 (Object ob) {this.ob = OB;    Public Object Getob () {return OB;    public void Setob (Object ob) {this.ob = OB;    The actual type of the public void Showtyep () {System.out.println ("T) is:" + ob.getclass (). GetName ()); }   }

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.