Comparison of Java generics with object

Source: Internet
Author: User

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

The benefit of generics is that it checks for type safety at compile time, and all casts are automatic and implicit, increasing the rate of reuse of the code. [Java]View Plaincopy print?
  1. Public class Gen<t> {
  2. private T ob; //define generic member variables
  3. Public Gen (T ob) {
  4. THIS.OB = OB;
  5. }
  6. Public T Getob () {
  7. return OB;
  8. }
  9. public void Setob (T ob) {
  10. THIS.OB = OB;
  11. }
  12. public void ShowType () {
  13. System.out.println ("T's actual type is:" + ob.getclass (). GetName ());
  14. }
  15. }
public class Gen<t> {private T ob;//define Generic member variable public Gen (T ob) {this.ob = OB;} Public T Getob () {return OB;} public void Setob (T ob) {this.ob = OB;} The actual type of public void ShowType () {System.out.println ("T" is: "+ ob.getclass (). GetName ());}}
[Java]View Plaincopy print?
  1. Public class Gendemo {
  2. public static void Main (string[] args) {
  3. //Define an integer version of generic class Gen
  4. gen<integer> Intob = new gen<integer> (88);
  5. Intob.showtype ();
  6. int i = Intob.getob ();
  7. System.out.println ("value=" + i);
  8. System.out.println ("----------------------------------");
  9. //Define a string version of generic class Gen
  10. gen<string> Strob = new Gen<string> ("Hello gen!");
  11. Strob.showtype ();
  12. String s = Strob.getob ();
  13. System.out.println ("value=" + s);
  14. }
  15. }
public class Gendemo {public static void main (string[] args) {//define an Integer version of generic class Gen gen<integer> Intob = new Gen< ;integer> (n); Intob.showtype (); int i = Intob.getob (); System.out.println ("value=" + i); System.out.println ("----------------------------------");//define a String version of generic class Gen gen<string> Strob = new Gen <String> ("Hello gen!"); Strob.showtype (); String s = Strob.getob (); System.out.println ("value=" + s);}}
[Java]View Plaincopy print?
  1. Public class Gen2 {
  2. private Object ob; //Define a common type member
  3. Public Gen2 (Object ob) {
  4. THIS.OB = OB;
  5. }
  6. Public Object Getob () {
  7. return OB;
  8. }
  9. public void Setob (Object ob) {
  10. THIS.OB = OB;
  11. }
  12. public void Showtyep () {
  13. System.out.println ("T's actual type is:" + ob.getclass (). GetName ());
  14. }
  15. }
public class Gen2 {private Object ob;//defines a common 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 public void Showtyep () {System.out.println ("T" is: "+ ob.getclass (). GetName ());}}
[Java]View Plaincopy print?
  1. Public class GenDemo2 {
  2. public static void Main (string[] args) {
  3. //Define an integer version of class Gen2
  4. Gen2 Intob = new Gen2 (new Integer (88));
  5. Intob.showtyep ();
  6. int i = (Integer) Intob.getob ();
  7. System.out.println ("value=" + i);
  8. System.out.println ("---------------------------------");
  9. //Define a string version of Class Gen2
  10. Gen2 Strob = new Gen2 ("Hello gen!");
  11. Strob.showtyep ();
  12. string s = (string) Strob.getob ();
  13. System.out.println ("value=" + s);
  14. }
  15. }
public class GenDemo2 {public static void main (string[] args) {//defines an Integer version of class Gen2 Gen2 Intob = new Gen2 (new Integer (88)) ; Intob.showtyep (); int i = (Integer) Intob.getob (); System.out.println ("value=" + i); System.out.println ("---------------------------------");//defines a string version of the class Gen2 Gen2 strob = new Gen2 ("Hello gen!"); Strob.showtyep (); string s = (string) Strob.getob (); System.out.println ("value=" + s);}}

Comparison of Java generics with object

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.