• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

Import Question

if I explicitly call my import statements :
  ie. import javax.swing.JPanel;
       import javax.swing.JSlider;
       ...

as opposed to :
   import javax.swing.*;

Will my program compile/run faster?
0
stalefish
Asked:
stalefish
  • 2
  • 2
1 Solution
 
imladrisCommented:
There can be an effect on compilation speed (though I have not noticed one in practice). There is definitely NO effect on running the program.
0
 
stalefishAuthor Commented:
Well whats actually happening here? Does the compiler go out and search for the appropriate class file if a ".*;" is found? Does this improve the compilation of the java file, or does it also improve the compilation of intermediate class file?
0
 
imladrisCommented:
It doesn't improve things in the sense of making them work better. Usually without an import statement of some kind, it doesn't work at all. When you reference the Vector class for instance you will need to import. You could do:

import java.util.Vector;

or

import java.util.*;

In the first case the the compiler will load the Vector class (to acquire the information about Vector that it needs to do the compilation). In the second case it will load all classes in java.util. Note that it is the compiler doing this, not the JVM. When the program is running under the JVM it only loads exactly what it needs, when it needs it.

Also again, in my experience, this does not cause a practical compilation delay (i.e. it has such minimal impact (in terms of actual time taken) there is no noticable difference between compiling files that import and those that don't).
0
 
conickCommented:
Importing a class or package is simply saving typing time.  It doesnt combine  (link) the package (library) with your classes like it would in C/C++.
I believe that it would effect compilation speed, but its negligible.  
Depending on how many classes in a package you use, I doubt that you would save enough compilation time to make up for the time you took to type all the classes that you use explicitly.
As imladris said, it wouldnt have any effect at all on program execution.

I dont have any benchmarks on this (I doubt anyone does).

My general rule is that if I use approx 2 or more classes in a package Ill import the whole package. But thats just me.
0
 
stalefishAuthor Commented:
Thanks alot
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now