What is the effect of redundant import statements on Java?

What happens with redundant java import statements?

Do they execute the compiled runtime (performance / size)? or just things like intellisense?

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

+6
source share
5 answers

Import operations only affect what happens at compile time.

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

After all, binaries are exactly the same, but the method for creating them is different.

Let's look at a simple case:

 import java.util.*; 

against

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

when used in:

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

When the compiler falls into the word List , in the first case it needs to find out if List exists in this set of classes or not. In the second case, this has already been given explicitly; therefore, it is much simpler.

In essence, what happens, the compiler must take all the classes that exist in the import statements and keep track of their names so that if you use it, the compiler can then get the corresponding functions that you call.

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

It is best to explicitly describe the use of your class.

+14
source

This does not affect performance in order to have redundant import statements. This can cause the source code to be longer than it should be, but there is no effect on the compiled code. Java itself imports unnecessary class files - see Java Language Specification Section 7.5.5 :

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

Section 7.5.2 states that

An on-demand import declaration never causes any other declaration to be obscured.

... means that importing wildcards will not exceed importing from one position.

As others have noted, any worthy IDE (NetBeans, Eclipse, etc.) will remove the unused imports.

+3
source

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

Like many performance issues, in this case the simplest and most understandable code is also the fastest.

You must maintain your import or maintain your IDE so that they are clear and easy to work with. The performance issue is very small, even for the compiler or IDE.

+2
source

The biggest danger is namespace collisions. If two imported libraries have a list type, for example, it may not use the one you think is.

+1
source

It is important to remove them because they add bloat to the .java file and because getting rid of them in the given file is quick and cheap, especially if you use an IDE ( CTRL - SHIFT - O , I think this is a shortcut in Eclipse).

As for β€œWhat makes excess imports for the car,” well, not so much. The class itself will be added only to the corresponding jar file once, and it will be loaded only once per class (see Not vis-a-vis "for each class"), therefore, in addition to adding some trivial amount of compilation time, it doesn’t will have a significant long-term effect for the program itself.

However, it’s cheap and easy to fix the problem: if you are not using an IDE, then you should have clearly grouped import statements that start with some sort of normal order (I alphabetize mine, which means that I β€œI will immediately see two import java.util.Map , because they will be next to each other!). Your coder colleagues will actively fulfill their duties if you do not fix it, so I suggest that it is in your interests.

+1
source

Source: https://habr.com/ru/post/904009/


All Articles