Details of generic example in java

  • 2020-11-20 06:03:56
  • OfStack

Introduction:

Prior to JavaSE1.5, parameters were "arbitrarilized" by referring to the type Object without generics. The disadvantage of "arbitrariness" was that it required explicit cast casting, which required the developer to do something predictable about the actual parameter type. In the case of a cast error, the compiler may not alert you to an error and an exception may occur only at run time, which is a security concern.

The benefits of generics are that type safety is checked at compile time, and all casts are automatic and implicit, increasing code reuse.

2. Generic parameters:


class Gen<T> {
	private T ob;
	// Define generic member variables 
	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("T The actual type is : " + ob.getClass().getName());
	}
}
public class GenericParameter {
	public static void main(String[] args){
		// Define a generic class Gen the 1 a Integer version 
		Gen<Integer> intOb=new Gen<Integer>(100);
		intOb.showType();
		int i= intOb.getOb();
		System.out.println("value= " + i);
		System.out.println("----------------------------------");
		// Define a generic class Gen the 1 a String version 
		Gen<String> strOb=new Gen<String>("Hello Dylan!");
		strOb.showType();
		String s=strOb.getOb();
		System.out.println("value= " + s);
	}
}

output:

The actual type of T is: ES17en.lang.Integer
value= 100
----------------------------------
The actual type of T is: ES26en.lang.String
value= Hello Dylan!

3. Generic classes:


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

4 Restricted 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(" Instantiate successfully !");
	}
}

output:
Instantiation successful!

5 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.

conclusion

The above is the whole content of the detailed explanation of the example of generic in java. I hope it will be helpful to you. If there is any deficiency, please let me know. Thank you for your support!


Related articles: