1

Possible Duplicate:
Any reason to clean up unused imports in Java, other than reducing clutter?

Please let me know if we put 1000 of import statement will it slow down execution of code?

Community
  • 1
  • 1
user492052
  • 151
  • 1
  • 2

3 Answers3

6

No, there is no performance penalty for import statements. They are not "executed" in runtime, they just help compiler to locate correct classes.

You can have zero import statements and rewrite all your class references to full class names (including packages), i.e. instead of:

import java.util.Collection;
import java.util.ArrayList;

Collection myColl = new ArrayList();

you can always write:

java.util.Collection myColl = new java.util.ArrayList();

This code is equivalent to the version above. It's just more verbose.

Peter Štibraný
  • 32,463
  • 16
  • 90
  • 116
1

It shouldnt, no - import statements are used for name resolution.

I suspect it may have an impact on compile time though.

At the end of the day, though - you need as many import statements as you need to unambiguously resolve all the classes you use - its not something you can easily 'optimise'

pauljwilliams
  • 19,079
  • 3
  • 51
  • 79
0

No it is even in most cases much better to write for instance:

import java.util.Collection;
import java.util.ArrayList;
import ...

then

import java.util.*;

Edit: for reasons of readability of your code and compiler performance.

Dave
  • 1,417
  • 14
  • 23
  • No matter how you write your imports, only classes that are used are actually loaded at runtime. Import statement is *not* use of class. – Peter Štibraný Nov 10 '10 at 13:09
  • @Peter: true. I meant to say for compiler time instead of runtime. The compiler will find the necessary classes faster :-). – Dave Nov 10 '10 at 13:23