Generic summary in Java of recommendation

  • 2021-07-10 19:58:06
  • OfStack

Java Generics (generics) is a new feature introduced in JDK 5. Generics provide a compile-time type safety detection mechanism, which allows programmers to detect illegal types at compile time.
The nature of a generic type is parameterized, meaning that the data type being manipulated is specified as one parameter.

Generic class

Example: Basic syntax of generic classes


class MyClass<T> {
	T value1;
}

Angle brackets < > T in is called a type parameter and is used to refer to any type. Actually you can write this T at will, but for specification purposes, Java still suggests that we use a single capital letter to represent the type parameter. Common ones such as:

T stands for any class of 1. E means Element, or Exception is abnormal. K stands for Key V stands for Value and is usually used in conjunction with K 1. S stands for Subtype, which will be explained later in the article.

Example: Generic classes introduce multiple type parameters and use


class MyClass<T,E> {
	T value1;
	E value2;
}
public class Test {
	public static void main(String[] args) {
		MyClass<String,Integer> myClass1 = new MyClass<String,Integer>();
	}
}

When the developed program can avoid downward transformation, it means that the potential safety hazard is eliminated. Try not to use downward transformation.
The emergence of generics has completely changed the need for downward transformation. After introducing generics, if the type is explicitly set, it is set; If the type is not set,
The default is Object type.

Generic method

Generics can be used not only to define classes, but also to define methods separately. As shown below:
Example: Generic method definition


class MyClass{
	public <T> void testMethod(T t) {
		System.out.println(t);
	}
}

Generic methods differ slightly from generic classes in that the type parameter, that is, the 1 part of angle brackets, is written before the return value. T in a method is called a type parameter, while T in a method is called a parameterized type, which is not a real parameter at run time.

Of course, the declared type parameters can also be regarded as the type of the return value.

Example: Generic method using type parameters as return values


class MyClass{
	public <T> T testMethod(T t) {
		return t;
	}
}

Adding generic definitions to program classes avoids the problem of ClassCastException, but creates a new problem: parameter unification.

Example: Observer


public class TestDemo {
	public static void main(String[] args) {
		Message<Integer> message = new Message() ;
		message.setMessage(99);
		fun(message); //  Error occurred, only receive String
	}
	public static void fun(Message<String> temp){
		System.out.println(temp.getMessage());
	}
}

Example: Using wildcards


public class TestDemo {
	public static void main(String[] args) {
		Message<Integer> message = new Message() ;
		message.setMessage(55);
		fun(message);
	}
	//  Wildcard characters are used at this time "?" Describes that it can accept any type, but it cannot be modified because the type is uncertain 
	public static void fun(Message<?> temp){
		//temp.setMessage(100);  Unable to modify! 
		System.out.println(temp.getMessage());
	}
}

Generic interface

Generics can be defined not only in classes, but also in interfaces, which we call generic interfaces.

Example: Defining a generic interface


interface IMessage<T> {
	//  Generics are defined on the interface 
	public void print(T t) ;
}

Example: Continuing to use generics when subclasses are defined


interface IMessage<T> {
	 //  Generics are defined on the interface 
	public void print(T t) ;
}
class MessageImpl<T> implements IMessage<T> {
	@Override
	public void print(T t) {
		System.out.println(t);
	}
}
public class TestDemo {
	public static void main(String[] args) {
		IMessage<String> msg = new MessageImpl() ;
		msg.print("Hello World");
	}
}

In the future, we will use generic interfaces in the program 1, which requires everyone to master.

Type erase

Generics were introduced in Java 1.5, and there was no concept of generics before that, but obviously, generic code is very compatible with previous versions of code.

This is because generic information exists only in the code compilation phase, and before entering JVM, generic-related information will be erased, which is called type erasure in technical terms.

Generally speaking, there is nothing special about generic classes and ordinary classes in java virtual machines.


Related articles: