0

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;
yossi
  • 12,945
  • 28
  • 84
  • 110
  • 1
    It won't impact performance, but it's a lot easier to maintain code that aren't using raw types. Another benefit is that you don't have to cast objects when you get items from the list. Do however note, that just changing from a raw type to List> isn't helping. – Kaj Jul 27 '11 at 08:05
  • 1
    Type erasure means the runtime performance is not impacted. – Ray Toal Jul 27 '11 at 08:06

5 Answers5

4

if i do not parameterize the list will the code run slower?

no

i mean will it have to do more checking at run time ?

no

The effect will be to increase readability of your code if you parameterize your classes, but everything is just compile time as long as you don't use any reflection stuff.

yankee
  • 38,872
  • 15
  • 103
  • 162
2

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.

Dmitri
  • 8,999
  • 5
  • 36
  • 43
1

The compiler can't help you if you don't parameterize your instances.

Using List<?> to avoid compile time checks is not advised unless you really don't know the type at compile time (there are occasions when this is the case, so I'm not saying to never use it).

Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

This definitely falls into the area of premature optimisation. The changes made by the compiler when adding type parameters to collections into the generated bytecode are just casts. When you write:

List<String> values = new ArrayList<String>();
values.add("One");
values.add("Two");

String value = values.get(0); // syntactic sugar

The compiler generates for the last line bytecode correspoding to

String value = (String) values.get(0);

So the hidden cost is the cost of casting. As pointed out by Tom Hawtin's answer to 'Does Java casting introduce overhead?':

That this sort of thing is largely irrelevant to performance.

which I tend to agree with - don't worry about the performance hit taken when using generics.

Community
  • 1
  • 1
Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278
1
  1. The compile time checking will reduce the possibility of errors at the run-time.
  2. While using iterator , will reduce the lines of code , as you don't require to explicitly cast.
  3. Its a good practice.
  4. This practice will enhance code readability.
  5. Also i feel you should adapt to newer versions (generics added in jdk 1.5)
Swagatika
  • 3,376
  • 6
  • 30
  • 39