Getting Started with Java Generics (interface)

Source: Internet
Author: User
Tags uppercase letter

A: Introduction to generics:

(1) The so-called generics, is the variable type of parameterization.

Generics are one of the most important features in JDK1.5. by introducing generics, we will gain the security of compile-time types and the possibility of running a smaller throw classcastexception . In JDK1.5, you can declare the type of object that a collection will receive/return. When using generics, if the parameter type is not specified, that is, the generic class is not parameterized, a warning is prompted, at which time the type is object.

(2) Why generics are used

A typical example of using generics is the use of generics in a collection. If you do not use generics , the elements stored in the collection can be of any type, and when taken out of the collection, all elements are of type object and require a downward coercion of type conversions to a specific type.

Like what:
List myintlist = new LinkedList (); 1
Myintlist.add (New Integer (0));//2
Integer x = (integer) myintlist.iterator (). Next (); 3
this forced type conversion of the third row may cause a run-time error.

The idea of generics is that the programmer specifies the type so that the collection can only accommodate elements of that type.
using generics:
list<integer> myintlist = new linkedlist<integer> (); 1 '
Myintlist.add (New Integer (0));//2 '
Integer x = Myintlist.iterator (). Next (); 3 '
Casting the coercion type of the third row into the list type description of the first row, the compiler checks the correctness of the type for us. In this way, the readability and robustness of the code are enhanced.

(3) Basic use of generics

Public interface List <E> {
void Add (E x);
Iterator<e> Iterator ();
}

Public interface Iterator<e> {
E next ();
Boolean hasnext ();
}
The angle brackets contain the formal type parameters (formal type parameters), which, like normal types, can be used in the declaration of the entire class. When the class is used, the specific actual type parameter (actual type argument) is used instead. For example List<integer&gt in the previous example, then all e will be replaced by the Integer type.
a generic type parameter can only be assigned to a class or interface type, cannot be assigned a value by a native data type, and the native data type needs to use the corresponding wrapper class.

Naming of formal type parameters: try to use a single uppercase letter (sometimes multiple generic types with numbers, such as T1,T2), such as many container collections using E, representing element (elements), and map with K for key keys,v to represent values.

Two practical applications:

(1) Creating a Java file for interface Parentinterface

Package Edu.tju.cs;public interface parentinterface<t1,t2>{public    void SetFoo1 (T1 foo1);    public void SetFoo2 (T2 foo2);    Public T1 getFoo1 ();    Public T2 GetFoo2 ();}

(2) Creating Java files that implement class ChildClass

Package Edu.tju.cs;public class Childclass<t1,t2> implements PARENTINTERFACE&LT;T1, t2>{private T1 foo1;        Private T2 Foo2;            @Override public void SetFoo1 (T1 foo1) {this.foo1 = foo1;    } @Override public void SetFoo2 (T2 foo2) {this.foo2 = Foo2;    } @Override Public T1 getFoo1 () {return this.foo1;    } @Override Public T2 GetFoo2 () {return this.foo2; public static void Main (string[] args) {childclass<string,string> cc = new childclass<string,string> (); cc . SetFoo1 ("Xu Qin"); Cc.setfoo2 ("Wang Zhaoji"); System.out.println ("Get ()" + cc.getfoo1 ()); System.out.println ("Get ()" + Cc.getfoo2 ()); childclass<integer,string> CC2 = new childclass<integer,string> () cc2.setfoo1 (123); Cc2.setFoo2 ("吴明静"); System.out.println ("Get ()" + cc2.getfoo1 ()); System.out.println ("Get ()" + Cc2.getfoo2 ()); childclass<string,number> cc3 = new childclass<string,number> () cc3.setfoo1 ("Shin Jiawei"); Cc3.setFoo2 ( 123.4556); SYstem.out.println ("Get ()" + cc3.getfoo1 ()); System.out.println ("Get ()" + Cc3.getfoo2 ());}}

Three Learning Experiences:

(1) Java generics can not have basic types

List<int> list=new arraylist<int> ();//Here, this is wrong.
List.add (0);
List.add (1);
This is because the generics require that the object type be inclusive, and the base type does not belong to the object in Java. But the base type has its wrapper type, which is the object type:
Int->integer
Long->long
....
so if you want to store the basic type, use its wrapper type to implement the function :
List<integer> list=new arraylist<integer> ();
List.add (0);
List.add (1);

In a word, generics can put <integer>, <character>;int,char basic type is not possible. In generics, you can put a reference type, such as an object type. <user>.

Http://docs.oracle.com/javase/tutorial/extra/generics/simple.html

Getting Started with Java Generics (interface)

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.