Search code examples
javagenericswarningsparameterized

java Type safety:not parameterized generic type effect on performance


i want to know if there is only safety risk or there are also other benefits i get when parameterizing the list.

i get this warning

List is a raw type References to generic type List should be parameterized

when declaring a list for example

List myList;

and if i change to this then there is no warning

List<?> myList;

i understand why there is the warning. i have a code with alot of those warnings and i want to know if it worth the time fixing all those warnings. my question is: if i do not parameterize the list will the code run slower ? i mean will it have to do more checking at run time ?

edit: and if i change to specific type will it then impact performance ?

List<String> myList;

Solution

  • All the benefits are in compile time checks, the running code will be identical. Having said that, you should probably bite the bullet and fix the code.

    Edit

    Specific types will also not impact performance one way or another. In short: generic type information is only available to the compiler (javac), which does not do any optimization (except for some really trivial, and irrelevant here, things). Optimization is done by the JVM and the JIT compiler, which do not have access to information about generic types - ergo, no performance difference.