General features of JDK5 and new features of jdk5
Generic is a special type that delays explicit work until an object is created or a method is called.
Generic Format:
<Data type>
Note: The data type here can be a reference type.
Benefits:
A: Advance the problem during the running period to the compilation period.
B: avoids forced type conversion.
C: Optimized Program Design
Import java. util. ArrayList;
Public class ArrayListDemo {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 ++) {String s = (String) array. get (x); // force type conversion System is required because no generic type is used. out. println (s );}}}
So where should generic be used?
We can check the API. If the class, interface, and abstract class are followed by <E>, it indicates that the generic type is used in the collection.
The origins of generics:
In the early days, we can use the Object type to receive any Object type, but in actual use, there is no problem with upward transformation, however, in the downward transformation, there is actually a implicit type conversion problem, there will be a forced type conversion problem, there is also a risk, therefore, Java has provided generics after JDK 5 to solve this security problem and improve program security.
Generic applications:
A: Generic
Is to define the generic type on the class.
Format: public class name <generic type 1,...>
Note: The generic type must be a reference type.
// Define 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); // during compilation, the ot cannot be reached. setObj (new String ("Lin Qingxia"); String s = ot. getObj (); System. out. println ("name:" + s); ObjectTool <Integer> ot2 = new ObjectTool <Integer> (); // ot2.setObj (new String (" ")); // during the compilation, the ot2.setObj (new Integer (27); Integer I = ot2.getObj (); System. out. println ("Age:" + I );}}
B: Generic Method
The generic type is defined on the method.
Format: public <generic type> return type method name (generic type)
Benefit: The method 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) {// ObjectTool ot = new ObjectTool (); ot. show ("hello"); ot. show (1, 100); ot. show (true );}}
C: generic interface
Is to define the generic type on the Interface
Format: public interface name <generic type 1...>
The implementation classes of generic interfaces are divided into the following two situations when implementing interfaces:
First case: You already know the type when implementing this interface.
// Define a generic interface Inter <T> {public abstract void show (T t );} // The implementation class of the generic class knows the type of public class InterImpl implements Inter on the implementation interface <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 ("hello ");}}
Case 2: the type of the interface is unknown when the interface is implemented.
// Define a generic class public interface Inter <T> {public abstract void show (T t );} // The 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 );}}
Wildcard advanced (wildcard)
Wildcard <?>
Indicates any type. If it is not clear, it is the Object and any Java class.
? Extends E
Down limit, E and its subclass
? Super E
Upper Limit, E and its parent class
Public class GenericDemo {public static void main (String [] args) {// If the generic type is explicitly written, collection must be consistent <Object> c1 = new ArrayList <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 of Collection can be used. <?> C5 = new ArrayList <Object> (); Collection <?> C6 = new ArrayList <Animal> (); Collection <?> C7 = new ArrayList <Dog> (); Collection <?> C8 = new ArrayList <Cat> ();//? Extends E: downward limit, E and its subclass // 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: upper limit, E is an extremely parent 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 {}