Is it more efficent for the compiler to import specific sub classes or more vague super classes? Classes that I'm Importing
Asked
Active
Viewed 72 times
-1
-
1What do you mean by import "more vague super classes"? – Pshemo Jun 26 '17 at 00:03
-
2If there's any difference, which is doubtful, it would hardly be measurable. But efficiency is the last thing you should be worrying about. Concern yourself first and foremost with code correctness and maintainability. – shmosel Jun 26 '17 at 00:03
-
1And please don't post text/code as image/link ([more info](https://meta.stackoverflow.com/a/285557)). Use [edit] option to correct your post. – Pshemo Jun 26 '17 at 00:03
-
Are you asking if you should, for instance, do `import java.sql.*` rather than `import java.sql.Driver` and `import java.sql.DriverManager` individually? – Kevin Anderson Jun 26 '17 at 00:05
-
Welcome to SO. Please edit your question to add more details. Right now it is hard to know what you are asking. – Gray Jun 26 '17 at 00:06
-
Related? [Why is using a wild card with a Java import statement bad?](https://stackoverflow.com/questions/147454/why-is-using-a-wild-card-with-a-java-import-statement-bad) – Pshemo Jun 26 '17 at 00:13
-
import is not class loading. They just allow you to type the short name for a class instead of the full class name. It's important to know what they're for and what they're doing. It's equally efficient for either one. Both save you keystrokes. That's all. – duffymo Jun 26 '17 at 00:41
-
"Efficient" with respect to what? – Lew Bloch Jun 26 '17 at 02:54
2 Answers
3
import statements don't require any runtime execution so there is no difference in efficiency. All that these imports do is establish a shorthand so that when you say 'File' the compiler knows you really mean java.io.File.

Evan Darke
- 604
- 4
- 7
-
-
@AndreAllen You should still clarify what you're asking for the benefit of others who may have the same question. – shmosel Jun 26 '17 at 01:46
-
To be more specific I was attempting to minimize the runtime for an application. I noticed that the IDE that I was using CodeRunner was importing more classes than needed, like (java.sql.*), when I only needed (java.sql.Connection). I wanted to know if there was an effect on the runtime of the program "efficiency", by importing the larger parent classes. – Andre Allen Jun 26 '17 at 15:22
1
The import
statement is pure syntactic sugar for the compiler. There is no difference in the resulting bytecode, thus no difference in runtime efficiency.
Instead, you should focus on what makes your code clearer to read - and in most cases (in my experience) more specific types are better (as oppose to wildcards.)

Michael Berry
- 70,193
- 21
- 157
- 216