Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the impact of redundant import statements in Java?

What is effected by redundant java import statements?

Do they effect the compiled runtime (performance/size)? or just stuff like intellisense?

To ask differently: how important is it to remove them?

like image 834
epeleg Avatar asked Dec 18 '11 06:12

epeleg


3 Answers

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.

like image 182
Kaushik Shankar Avatar answered Oct 13 '22 00:10

Kaushik Shankar


It doesn't impact performance to have excess import statements. It may make the source code longer than it should be but there is no effect on the compiled code. Java itself imports unnecessary class files - see the Java Language Specification section 7.5.5:

Each compilation unit automatically imports all of the public type names declared in the predefined package java.lang, as if the declaration:
import java.lang.*;
appeared at the beginning of each compilation unit, immediately following any package statement.

Section 7.5.2 says that

A type-import-on-demand declaration never causes any other declaration to be shadowed.

...meaning that wildcard imports won't trump single-entry imports.

As others have pointed out, any decent IDE (NetBeans, Eclipse, etc) will remove unused imports for you.

like image 45
Paul Avatar answered Oct 12 '22 23:10

Paul


Like many questions about performance, clarity of the code is usually more important. This should be your first thought, and only in the rare cases where you have a known (measured) performance problem you should consider not writing the simplest and clearest code you can.

Like many performance questions, in this case the simplest, clearest code is also the fastest.

You should maintain your import or have your IDE maintain them, to keep them clear and make you code easier to maintain. The performance issue is very small, even for the compiler or IDE.

like image 43
Peter Lawrey Avatar answered Oct 12 '22 23:10

Peter Lawrey