Automatic imports : are they inefficient ?

Both snippets have the same behaviour :

import java.util.ArrayList;
public class A {
    public static void main(String[] argv) {
        new ArrayList();
    }
}
import java.util.*; // automatic import
public class A {
    public static void main(String[] argv) {
        new ArrayList();
    }
}

But we can be curious about efficiency.
In fact the same bytecode will be produced :

[kaz@akina]$ cat A.jad
import java.util.ArrayList;
public class I {
    public static void main(String args[]) {
        new ArrayList();
    }
}

Automatic imports are processed at compile time, thus no difference exists at runtime.
However, compilation will be probably a bit slower.

Advertisements

3 thoughts on “Automatic imports : are they inefficient ?

  1. What do you think of actually *choosing* what we do (or not) want to import from a given package ? In haskell for example we can do :

    import Data.List (head, map)
    which only imports the head and map functions from the Data.List module

    import Data.List hiding (head, map)
    which imports the whole module except the head and map functions

    import qualified Data.List
    which imports the whole module but requires us to write Data.List.map for example, each time we refer to it

    import qualified Data.List as DL
    which imports the whole module but requires us to write DL.map for example, each time we refer to it

    which is quite similar to C++’s namespaces actually.

    Despite how simple such topics look, there are many ideas one can think of to get better modularity, power and all at the package/module level.

    • In Java, qualified imports are implicits. In fact you always can do :
      new org.package.subpackage.MyClass(); // Of course, this class must be in the classpath.

      Refer to the post : even if you import all classes from a package with “*”, only the needed classes will be import. In fact, it’s javac which write needed imports.
      However your hidding imports doesn’t exist in java, but since the “*” don’t really import all classes in the package, this feature is less useful in java.
      In Haskell you have more features about imports, but in fact java imports are totally sufficient.
      Besides with the *, imports are often very easy to use and imports blocks are short.

  2. Thanks a lot! I’ve been reluctant for a while to use automatic imports since I did not really know whether they would impact efficiency. Thanks to you, now I know: they don’t.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s