Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the reasons to optimize imports?

Are there any technical reasons to "optimize imports" via your editor? Eclipse, Intellij IDEA, and NetBeans all have ways to optimize imports. I'm wondering if there are reasons other than consistency. Also, is there a more optimal way of importing? I've seen different standards that individuals and orginations have for optimizing imports. For example...

import java.util.Map;
import java.util.List;

import com.company.MyClassThatUsesMap;

If I understand right, in the above example the classloader will load the Map and List classes before MyClassThatUsesMap. Will this add any benefit to the speed at which the code will run versus the example below?

import com.company.MyClassThatUsesMap;

import java.util.List;
import java.util.Map;

Does this even matter at all or does the compiler fix it altogether?

like image 414
Gordolio Avatar asked May 07 '15 13:05

Gordolio


2 Answers

If I understand right, in the above example the classloader will load the Map and List classes before MyClassThatUsesMap.

You don't understand right. Imports have nothing to do with execution-time handling. They only affect how the compiler resolves short names (e.g. Map) into fully-qualified class/method names.

The aim of "optimizing" imports is for readability - not execution-time performance.

like image 188
Jon Skeet Avatar answered Oct 04 '22 21:10

Jon Skeet


There's no difference. It's only for the developer's eyes, although most IDEs hide the imports since the developer is rarely interested in seeing them.

like image 34
Kayaman Avatar answered Oct 04 '22 20:10

Kayaman