Java Import VS Code Performance

Java import vs code performance

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.

Do unused imports in java effects performance and consume memory?

No, imports are a compile-time feature (they have no meaning in the compiled code). They do not affect runtime behavior.

Performance difference between a wild card import and the required class import

At runtime 0.

Both generate the same byte code

Do unused import and objects have a performance impact?

Its a very common question.

Like most performance questions the best approach is to write the clearest and simplest code you can as this improves the maintainability of the code and helps ensure it performs reasonably well even after it is changed. (Clever/Obtuse/Needlessly Verbose code can run fast to start with but as it is changed by mere mortals it can get much slower)

Unused imports have a trivial impact on the compiler, but there are no imports in the byte code or at runtime.

Unused objects can be optimised away, but its best to avoid these as they almost always cause some performance impact, but more importantly make reading and maintaining your code more difficult.

Import package.* vs import package.SpecificType

There is not a performance or overhead cost to doing import .* vs importing specific types. However, I consider it to be a best practice to never use import .* My primary reason for this is I just like to keep things straightward, clean and with as little ambiguity as possible, and I think with a .* import you lose that.

what is the impact of redundant import statements in Java?

Import statements only affect what happens during compile time.

The compiler takes this code, and creates a .class file that represents your code in an executable format (something in binary).

In the end, the binaries are exactly the same, but the method by which they are made are different.

Let's look at a simple case:

import java.util.*;

vs

import java.util.ArrayList;
import java.util.List;

when used in:

//...
List <String> someList = new ArrayList <String> ();
//...

When the compiler hits the word List, in the first case, it will need to figure out if List exists in that set of classes or not. In the second case, it is already given it explicitly, so its much easier.

In essence, what happens is the compiler must take all the classes existing in the import statements and keep track of their names so that, if you use it, the compiler can then retrieve the appropriate functions that you are calling.

Sometimes, there are classes that have the same name in multiple packages. It is in this case (which Thomas is referring to) that you should not use the * to select all the classes in the directory.

It is best practice to explicitly describe your class usage.

What is more efficient in importing in Java?

import statements create a compiler-time alias to the symbol imported. That is, it's just a shortcut for typing the full name out - it has no effect on the program while it's running. The compiled code is identical in both cases.

Does import statement increase the size of a class

It does not change anything at all. The import statements are just there to help the compiler create the necessary bytecode; in the bytecode itself, all class references are fully qualified and linkage is done at runtime, always (this is what a classloader is all about).

Now, depending on coding styles, "star imports" may be frowned upon...



Related Topics



Leave a reply



Submit