1. Introduction

Java 1.5 introduced generics to ensure type safety, prevent type conversion exceptions at runtime, parameterize types, and improve code readability and reuse. But there are situations in which generics are not allowed, and today I’ll summarize some of the situations in which generics are not allowed in code.

2. Basic types cannot use generics directly

The following is incorrect:

// error 
Map<int.char> wrong= new HashMap<>()
Copy the code

Primitive types are not generic types and need to use their corresponding wrapper classes.

// OK
Map<Integer,Character> wrong= new HashMap<>()
Copy the code

3. Generic types cannot be instantiated directly

A generic type can be understood as an abstract type, representing only an abstraction of the type, so we cannot instantiate it directly. It is also wrong to:

 public <E> E first(List<E> list){
     // error 
        E e = new E();
       return list.get(0);   
 }
Copy the code

4. Generics cannot be used as static variable types

Static types in Java are instantiated as the class is loaded; the specific type of the generic type is not declared. And because static variables are shared by all objects, their types cannot be determined until class instantiation or method invocation. The type cannot be determined if it is a generic type. A generic type declared on a class cannot be used as a return value type in a static method of the class.

public class Generic<T>{
    // A generic type declared by a class cannot be a static variable
    public static T t;
    // A generic type declared by a class cannot be returned as a static method
    public  static  T  rtval(List<T> list){
        return list.get(0); }}Copy the code

5. Inability to instanceof judgment

Generics in Java are pseudo-generics and are erased at compile time. Generics do not exist in the bytecode run, so the following criteria cannot be applied:

public static <E> void wrong(List<E> list) {
    // error 
    if (list instanceof ArrayList<Integer>) {   
    }
}
Copy the code

But the unbounded wildcard
< p style =” max-width: 100%; clear: both;

6. Cannot create array of parameterized type

First of all, the following is correct:

// OK
List[] arrayOfLists = new List[2];
Copy the code

But adding generics will not compile:

//error
List<Integer>[] arrayOfLists = new List<Integer>[2];
Copy the code

Failure to do so raises the following logical error:

// If the above is true, then the following should also be true
Object[] stringLists = new List<String>[];  
// Then we can put the string List
stringLists[0] = new ArrayList<String>();   
// Put an Integer list
stringLists[1] = new ArrayList<Integer>();
// This is obviously unreasonable
Copy the code

7. Throwable cannot be extended directly or indirectly

Two ways to write this will cause a compilation error:

// Throwable cannot be extended indirectly
class IndirectException<T> extends Exception {}     

// Throwable cannot be extended directly
class DirectException<T> extends Throwable {} 
Copy the code

If true:

 try {
        // ...
    } catch (T e) {   
        // The type is uncertain and cannot handle specific exception logic
    }
Copy the code

How you can handle exceptions in detail is obviously not conducive to precise exception-handling logic. You can throw an indeterminate exception, but you can’t use class-declared generics in static methods either:

class Parser<T extends Exception> {
   // That's right
    public void okThrow(File file) throws T {      
        // ...
    }
    Static methods cannot have class-declared generic types as return values and exceptions
    public static void wrongThrow(File file) throws T {}}Copy the code

8. Methods with the same parameter signature after generic erasure cannot be overridden

Due to generic erasure, the following are not considered method overloads and do not compile:

public class NoReload {
    public void sets(Set<String> strSet) {}public void sets(Set<Integer> intSet) {}}Copy the code

9. To summarize

Today, I summarized some of the pitfalls of Using Java generics, which are often overlooked despite the usual IDE tips. If there is any deficiency, please comment on it. If you want to learn more about generics, you can check out generic for study notes.

Follow our public id: Felordcn for more information

Personal blog: https://felord.cn