newbie newbie - 6 months ago 13
Java Question

Java import vs code performance

I am wondering if i included many

import
in my java program, would it affect the performance of my code (for example, program will be slower)? Is the logic behind the
import
in Java the same as
include
in C?

Answer

would it affect the performance of my code (for example, program will be slower)?

No, it wouldn't affect performance of your code.

The binaries (the class files) do not increase in size as the import is not implemented with any cut-and-paste mechanism.

It is merely a syntactic sugar for avoiding to have to write for instance

java.util.List<java.math.BigInteger> myList =
        new java.util.ArrayList<java.math.BigInteger>();

Here is a little test demonstrating this:

aioobe@e6510:~/tmp$ cat Test.java 
import java.util.*;

public class Test {
    public static void main(String[] args) {
        List<Integer> myInts = new ArrayList<Integer>();
    }
}
aioobe@e6510:~/tmp$ javac Test.java
aioobe@e6510:~/tmp$ md5sum Test.class 
523036e294b17377b4078ea1cb8e7940  Test.class

(modifying Test.java)

aioobe@e6510:~/tmp$ cat Test.java 


public class Test {
    public static void main(String[] args) {
        java.util.List<Integer> myInts = new java.util.ArrayList<Integer>();
    }
}
aioobe@e6510:~/tmp$ javac Test.java
aioobe@e6510:~/tmp$ md5sum Test.class 
523036e294b17377b4078ea1cb8e7940  Test.class

Is the logic behind the import in Java the same as include in C?

No, an #include is a preprocessor directive and is implemented with a cut-and-paste mechanism.

Comments