One introduction:
Before JavaSE 1.5, without generics, the "arbitraryization" of parameters was achieved by referencing the type Object. The disadvantage of "arbitraryization" was that explicit cast type conversion was required, and this conversion required developers to predict the actual parameter type. In case of cast type conversion errors, the compiler may not prompt an error, and an exception will only occur when running, which is a security risk.
The advantage of generics is that they check type safety when compiling, and all casts are automatic and implicit, improving the reuse rate of code.
2. Generic parameters:
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;}public void showType() {System.out.println("The actual type of T is: " + ob.getClass().getName());}}public class GenericParameter {public static void main(String[] args){//Define an Integer version of the generic class Gen Gen<Integer> intOb=new Gen<Integer>(100);intOb.showType();int i= intOb.getOb();System.out.println("value= " + i);System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- output:
The actual type of T is: java.lang.Integer
value= 100
--------------------------------------------------------------------------------------------------------------------------------
The actual type of T is: java.lang.String
value= Hello Dylan!
3. Generics:
class GenericsFoo<T> {private T x;public GenericsFoo(T x) {this.x = x;}public T getX() {return x;}public void setX(T x) {this.x = x;}}public class GenericClass {public static void main(String args[]){GenericsFoo<String> strFoo=new GenericsFoo<String>("Hello Generics!");GenericsFoo<double> douFoo=new GenericsFoo<double>(new double("33"));GenericsFoo<Object> objFoo=new GenericsFoo<Object>(new Object());System.out.println("strFoo.getX="+strFoo.getX());System.out.println("douFoo.getX="+douFoo.getX());System.out.println("objFoo.getX="+objFoo.getX());}} output:
strFoo.getX=Hello Generics!
douFoo.getX=33.0
objFoo.getX=java.lang.Object@1d0fafc
Four-limited generics:
import java.util.ArrayList;import java.util.Collection;class CollectionGenFoo<T extends Collection> {private T x;public CollectionGenFoo(T x) {this.x = x;}public T getX() {return x;}public void setX(T x) {this.x = x;}}public class GenericRestrict {public static void main(String[] args) {CollectionGenFoo<ArrayList> listFoo = null;listFoo = new CollectionGenFoo<ArrayList>(new ArrayList());CollectionGenFoo<? extends Collection> listFoo1 = null;listFoo1=new CollectionGenFoo<ArrayList>(new ArrayList());System.out.println("Instantiation successful!");}} output:
Instantiation was successful!
Five generic methods:
public class GenericFunction {public <T> void f(T x) {System.out.println(x.getClass().getName());}public static void main(String[] args) {GenericFunction ea = new GenericFunction();ea.f(" ");ea.f(10);ea.f('a');ea.f(ea);}} output:
java.lang.String
java.lang.Integer
java.lang.Character
GenericFunction
--------------------------------------------------------------------------------------------------------------------------------
dylan presents.
Summarize
The above is all the content of this article about the detailed explanation of generic examples in Java, I hope it will be helpful to everyone. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!