It is known that in Java generics are implemented on the basis of the type-erase model, that is, if there is a class
class CatContainer <T>{ T[] names; public CatContainer(){ names=(T[]) new Object[10]; } } Then at runtime, the program replaces the parameterized type with the type Object , that is, the program has no information about the actual type of the parameter T
Hence the question, if you create a container object as follows:
CatContainer<String> cats=new CatContainer<>(); Why with this operation
cats.names[0]=5; an error occurs?
Type mismatch: cannot convert from int to String
I suppose that in this case there is a preliminary (compile time) verification of the correctness of a program, during which other protection mechanisms are used, rather than when checking the correctness of a program at run time (run time). But I do not have a clear understanding of how this happens.