ikhebgeenaccount
ikhebgeenaccount

Reputation: 373

What is better: import one by one or .*?

I am busy with a little Java program for a school project, and my import list became longer and longer. This made me wonder if it is better to import all at once:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

Or to import necessary classes one by one:

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;

import java.awt.event.ComponentListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Point;

I can imagine, with really big projects, the import list can become really long. So are there any performance considerations?

Found answer here, link provided by Guo Song:

The import directive is a compiler directive, it tells the compiler where to look for a class and allows to not have to always use fully qualified class names, e.g. java.util.HashMap. But the import directives themselves do not get put into the compiled bytecode files, the compiler compiles the fully qualified name into the .class file.

When used wiithout a wildcard, the directive explicitly tells the compiler to look for one specific file in the classpath. With a wildcard, the directive tells the compiler to look for the named package and to search in that package for possible matches every time any name needs to be matched. The latter version is probably going to take (a bit) longer for the compiler than the former.

In other words, the import directive cannot affect runtime code execution in any way. However, the import directive does affect compilation time. Additionally, I find that using import with wildcards makes the code less readable.

Actually, the cost of import statements question of the month on javaperformancetuning.com perfectly summarize this in its conclusion:

There is no runtime cost from using an import statement The compilation process can take a little more time with an import statement The compilation process can take even more time with a wildcard import statement For improved readability, wildcard import statements are bad practice for anything but >>throwaway classes The compilation overhead of non-wildcard import statements are minor, but they give readability benefits so best practice is to use them

Upvotes: 1

Views: 1759

Answers (4)

Sharad
Sharad

Reputation: 446

  1. Best programming practice says, if we know which class we need we should import that only instead of all list of classes present in the package.
  2. Unused imports do not have a impact at runtime (because there are no imports in the byte code). However, unused imports will affect the compiler, but not by much.

Upvotes: 0

Sumit Jhajharia
Sumit Jhajharia

Reputation: 135

It will not effect the RUN TIME SPEED of program.

but it will effect COMPILE TIME SPEED of program.

if you import java.sql.* then at the compile time compiler import packages one by one so there is a overhead in compile time

But at run time compiler ignore all the unused import statement that is imported by compiler at compile time so it will not effect RUN TIME speed of your program.

generally it is good to import by full qualified name instead of importing all list ,this will increase readability ,maintainability of your code .

Let's look at a simple case:

     import java.util.*;

and

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



     //.when we write a program :..
     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.

Upvotes: 2

Alastair Campbell
Alastair Campbell

Reputation: 668

It is better to import them one by one.

This is simply because you may not have code collisions now, but if there is a later update to the package you are importing that adds a new class that has the same name as a class you have personally, written, it will break.

Performance is not really the main issue with this, because it shouldn't make the code run slower, but it may increase the size of your .class files or .jar.

The number of import statements is not really that big of an issue - yes, it may be many lines, but does it really do any damage to the readability of your code? Not really.

I hope this helped.

Upvotes: 1

Rodrigue Abou Naoum
Rodrigue Abou Naoum

Reputation: 3

In My Opinion , You should import them one by one :

  • Importing them all will result many imports that you don't need
  • Simple reason also that you can see them in front of you as makes it easier to you and you can also arrange them and put comments on top for educational purpose or other..

Upvotes: 0

Related Questions