Generics of Java Foundation (Learning Notes)

Keywords: Java

generic paradigm
A disadvantage of Java collections is that if an object is dropped into a collection, the collection will forget the data type of the object, and the type of the object will become an Object type. The Java collection is designed so that the designer of the collection does not know what the type of storage is to be, so that the universal design is Obejct.
In order to solve this problem, the concept of generics is designed. Generics are those that specify the type of a collection when it is created.
1. Use of generics

import java.util.ArrayList;
import java.util.List;

public class GenericList {
	public static void main(String[] args) {
		//The act of specifying the type of collection when creating a collection is generic.
		List<String> strList =new ArrayList<String>();
//		strList.add(23);

2. Java 7 generic diamond statements
The data type behind the diamond syntax in Java 7 can be omitted and the system automatically determines the data type.
For example: List books = new ArrayList <>();
3. Define generic classes and generic interfaces

public class Apple<T> {

	// Define instance variables with parameters of type T
	private T info;

	public Apple() {

	public Apple(T info) {;

	public T getInfo() {
		return info;

	public void setInfo(T info) { = info;
	public static void main(String[] args) {
		Apple<String> al =new Apple<String>("Apple");
		Apple<Double> a2 =new Apple<>(3.21);

4. Derivation of subclasses from generic classes

public class DemoA<T> extends Apple<T> {
	//Subclasses inherit generic parent classes that need to specify data types or continue to use generics
	public T getInfo() {
		return super.getInfo();
public class DemoB extends Apple<String> {
	public String getInfo() {
		return "Subclass"+super.getInfo();

5. Generic Approach

import java.util.ArrayList;
import java.util.Collection;

public class GenericMethodTest {
	//Store data in an array into a collection
	public static void fromArrayToCollection(Object [] arr,Collection<Object> coll) {
		for (Object obj : arr) {
	//Using generics to implement the functions described below is a generic approach
	public static  <T>  void fromArrayToCollectionGeneric(T [] arr,Collection<T> coll) {
		for(T o:arr) {
	public static void main(String[] args) {
		Object [] oa =new Object[100];
		Collection<Object> co =new ArrayList<>();
		//The following code T represents the Object type
		String [] sa =new String[100];
		Collection<String> cs =new ArrayList<>();
		//The following code T represents the String type
		Integer [] ia =new Integer[100];
		Float [] fa =new Float[100];
		Number [] na =new Number[100];
		Collection<Number> cn =new ArrayList<>();
		//The following code T represents Number type
		//The following code T represents the Object type
		//The following code reports errors, where T represents String type and na is NUmber type, not a subclass of String, so the error is reported
//		fromArrayToCollectionGeneric(na,cs);

6. Generic Erasion and Conversion
Generics in Java are pseudo-generics, which are valid only at compile time and do not exist at run time.
When an object with generic information is assigned to another variable without generic information, all type information between pointed brackets is thrown away.

public static void main(String[] args) {
		Apple<Integer> a =new Apple<Integer>(6);
		//as will get the collection size
		Integer as =a.getSize();
		//Assigning an object a to the apple variable b will lose the type information in angle brackets 
		Apple b=a;
		//b Just getSize returns a Number type, so no error is reported.
		Number as2=b.getSize();
		//The following code will report errors because the data type is missing from assigning b.
//		Integer as3 =b.getSize();

Posted by Justin L H on Tue, 08 Oct 2019 05:37:36 -0700