Java Basics Note: generics

Source: Internet
Author: User

Content: Generics in Java


Excellent article:
Sina Blog-Ocean Planet: http://blog.sina.com.cn/s/blog_44c1e6da0100cus3.html
Books: Java, crazy Java handouts

Description
Generics are new features added to Java SE1.5;
The essence of generics is a parameterized type, which is simply to treat generics as one or more parameters, and the code fragment using this parameter can be replaced by any specific class type parameter;
A generic parameter type can only be a class type, not a simple type (simple type is a programming language built-in data type that can no longer be simplified, there are real-float-double in Java, Integer-byte-short-int-long, character-char, Boolean-boolean) ;
Generics can be used in the definition of interfaces (member properties), classes (member properties), methods (return values), which are called generic interfaces, generic classes, generic methods, respectively;
The use of generics can improve the reusability of code;
Generic information is mainly used in the compilation phase, and for runtime, generic information is lost (type erase);
Generics are commonly used with collections;


Use:
1. Ensure the legitimacy of the data (the stored data is not judged when the elements are stored in the collection, that is, different data types can be stored in the set without distinction);
2. The type of the collection element specifies that the stored elements in the collection are OB types by default, so data conversions are required for each value, and the data type of each element is clearly known;

1 Importjava.util.LinkedList;2 Importjava.util.List;3 4 5  Public classDemolist {6     //Traditional Programs7      PublicList getList01 () {8List List =NewLinkedList ();9         //any value can be stored in the set, if the value of the conversion error, the exception is generated;TenList.add ("One"); OneList.add (2); A         returnlist; -     } -     //using generics the      PublicList<string>getList02 () { -list<string> list =NewLinkedlist<string>(); -         //The non-parameter type value is not saved, which restricts the element type of the collection; -List.add ("One"); +List.add ("Tow"); -         returnlist; +     } A  at      Public Static voidMain (string[] args) { -         //TODO auto-generated Method Stub -Demolist dl =Newdemolist (); -List L1 =dl.getlist01 (); -         //a strong turn is required for each value -String s0 = (string) l1.get (0); in         intS1 = (int) L1.get (1); -System.out.println (s0+s1); to          +list<string> L2 =dl.getlist02 (); -         //No conversion is required when the value is taken theString s3 = L2.get (0); *String S4 = L2.get (1); $System.out.println (s3+S4);Panax Notoginseng     } -}


Type erasure in generics
In Java, there is no generic type object, and the JVM does not support generic types at all, and generic classes are generated after the JVM compiles. class files are primitive types, generic type parameter lists are removed, and the JVM replaces type parameters with the qualified type of the type parameter;
Because of the type erasure, so:
You cannot replace a primitive type with a type parameter;
The Run-time type check checks only the original type of the generic class;
Objects that cannot be thrown and cannot capture generic classes;
An array of parameterized types cannot be declared;
You cannot instantiate an object that does not have a generic type set;
Type variables cannot be referenced in static or static methods;
The generic method conflicts with the same name as the original method;

1 Importjava.util.LinkedList;2 Importjava.util.List;3 4 5  Public classTset {6 7      Public Static voidMain (string[] args) {8         //TODO auto-generated Method Stub9List L1 =NewLinkedList ();Tenlist<string> L2 =NewLinkedlist<string>(); Onelist<stringbuilder> L3 =NewLinkedlist<stringbuilder>(); A         if(L1.getclass () = =L2.getclass ()) { -System.out.println ("L1 is the same as L2 type"); -System.out.println ("L1 type is" +L1.getclass (). toString ()); theSystem.out.println ("L2 type is" +L2.getclass (). toString ()); -         } -         if(L1.getclass () = =L3.getclass ()) { -System.out.println ("L1 is the same as L3 type"); +System.out.println ("L1 type is" +L1.getclass (). toString ()); -System.out.println ("L3 type is" +L3.getclass (). toString ()); +         } A         if(L2.getclass () = =L3.getclass ()) { atSystem.out.println ("L2 is the same as L3 type"); -System.out.println ("L2 type is" +L2.getclass (). toString ()); -System.out.println ("L3 type is" +L3.getclass (). toString ()); -         } -          -     } in  -}

Generics and parameter types
Generics are based on the class itself, not on parameter types, that is, two generic class objects that have an inheritance relationship are incompatible;

1     New Linkedlist<number>(); 2     New Linkedlist<byte>(); 3     list = List2; // Error!  

Use the type wildcard character <?> to make generics borderless, using Extends<? Extends parent class > or super<? Super Parent class > can qualify generic type;

1      Public void printlist (list<?> List); // receive any parameter type; 2      Public void extends number> list); // receive number or number subtype; 3      Public void Super number> list); //

Java Basics Note: generics

Related Article

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.