JDK5 new characteristics of the generic type

Source: Internet
Author: User

Generics are a special type of explicit work that is deferred until the object is created or when the method is called.

The format of the generic type:

< data types >

Note: The data type here can be a reference type

Benefits:

A: Advance the run-time problem to the compile period

B: Forced type conversions are avoided

C: Optimization of program design

 import java.util.ArrayList; 
Public class Arraylistdemo { Span style= "color: #0000ff;" >public static void main (string[] args) {ArrayList array = new ArrayList (); Array.add ( "Hello" ); Array.add ( "World" ); Array.add ( "java" for (int x = 0; x < array.size () X++ =

So where do generics work?

We can look at the API, if the classes, interfaces, and abstract classes are followed by <E> to use generics, which is generally used in collections.

The origin of generics:

In the early days we can use object type to receive arbitrary object types, but in the actual use of the process, there is no problem of upward transformation, but in the downward transformation of the fact that the type conversion problem, there will be forced type conversion problems, there is a hidden danger, Therefore, Java has provided generics to solve this security problem and improve the security of the program after JDK5.

Generics for Applications:

A: Generic class

is to define generics on the class

Format: public class class name < generic type 1,... >

Note: The type of the generic must be a reference type

Defines a generic class Objecttool<t> {private T obj;public T Getobj () {return obj;} public void Setobj (T obj) {this.obj = obj;}} Define a test class public class Objecttooldemo {public static void main (string[] args) {objecttool<string> ot = new Objecttool <String> ();//Ot.setobj (new Integer (27)); Ot.setobj ("Brigitte") is not a time-out during compilation. String s = ot.getobj (); System.out.println ("name is:" + s);objecttool<integer> Ot2 = new objecttool<integer> ();//Ot2.setobj (new String ("Wind"));//This time the compilation will not be Ot2.setobj (new integer); Integer i = Ot2.getobj (); System.out.println ("Age is:" + i);}}

B: Generic method

is to define generics on the method.

Format:public< generic type > Return type method name (generic type)

Benefit: Methods can receive any type of data

public class Objecttool {    //define a generic method public <T> void Show (T t) {System.out.println (t);}} Define a test class public class Objecttooldemo {public static void main (string[] args) {//define a generic method after objecttool ot = new Objecttool (); O T.show ("Hello"); Ot.show (+); Ot.show (True);}}

C: Generic interface

is to define generics on the interface.

Format: public interface interface name < generic type 1...>

The implementation class of a generic interface has the following two scenarios when implementing an interface

The first case: when implementing the interface, you already know what type

Defines a generic interface interface inter<t> {public abstract void Show (T-t);} The implementation class of a generic class implements an interface to know what type of public class Interimpl implements inter<string> {@Overridepublic void show (String t) { System.out.println (t);} }//test class public class Interdemo {public static void main (string[] args) {inter<string> i = new Interimpl (); I.show ("Hel Lo ");}}

Second case: When implementing the interface, you don't know what type it is.

Defines a generic class public interface inter<t> {public abstract void Show (T t);} The interface is implemented without knowing what type of public class Interimpl<t> implements inter<t> {@Overridepublic void show (T t) { System.out.println (t);}} Test class public class Interdemo {public static void main (string[] args) {inter<string> i = new interimpl<string> () ; I.show ("Hello");inter<integer> II = new interimpl<integer> (); ii.show (100);}}

Generic Advanced (wildcard character)

Generic wildcard characters <?>

Represents any type, and if it is not, it is object and any Java class.

? Extends E

Down qualification, E and its subclasses

? Super E

Limit up, E and its parent class

public class Genericdemo {public static void main (string[] args) {//generics must be consistent collection<object> C1 = new If explicitly written Rraylist<object> ();//collection<object> C2 = new arraylist<animal> ();//error//Collection<object > c3 = New arraylist<dog> ();//error//collection<object> C4 = new arraylist<cat> ();//error//? Indicates that any type is possible collection<?> c5 = new arraylist<object> (); collection<?> C6 = new arraylist<animal> (); Collection<?> c7 = new arraylist<dog> (); collection<?> C8 = new arraylist<cat> ();//? Extends e: down limit, E and its subclasses//collection<? Extends animal> C9 = new arraylist<object> ();//error collection<? Extends animal> C10 = new arraylist<animal> (); collection<? Extends animal> C11 = new arraylist<dog> (); collection<? Extends animal> C12 = new arraylist<cat> ();//? Super E: Limit up, E extremely parent class collection<? Super Animal> C13 = new arraylist<object> (); collection<? Super Animal> C14 = new arraylist<animal> ();//collection<? Super Animal> C15 = new arraylist<dog> ();//error//collection<  Super Animal> C16 = new arraylist<cat> ();//Error}}class Animal {}class Dog extends Animal {}class Cat extends Animal {}

  

JDK5 new characteristics of the generic type

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.