Performance Difference Between a Wild Card Import and the Required Class Import

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

At runtime 0.

Both generate the same byte code

Why is using a wild card with a Java import statement bad?

The only problem with it is that it clutters your local namespace. For example, let's say that you're writing a Swing app, and so need java.awt.Event, and are also interfacing with the company's calendaring system, which has com.mycompany.calendar.Event. If you import both using the wildcard method, one of these three things happens:

  1. You have an outright naming conflict between java.awt.Event and com.mycompany.calendar.Event, and so you can't even compile.
  2. You actually manage only to import one (only one of your two imports does .*), but it's the wrong one, and you struggle to figure out why your code is claiming the type is wrong.
  3. When you compile your code, there is no com.mycompany.calendar.Event, but when they later add one, your previously valid code suddenly stops compiling.

The advantage of explicitly listing all imports is that I can tell at a glance which class you meant to use, which simply makes reading the code much easier. If you're just doing a quick one-off thing, there's nothing explicitly wrong, but future maintainers will thank you for your clarity otherwise.

Coding style advantages in importing individual classes over importing by wildcard (star)

There are no runtime performance differences. It mainly stems down to being able to understand what was used.

You are right if you make use of, say 75% of the calls in a package it's probably best to import that entire package with wildcard. Whereas if you only make use of 1 or 2 out of 100 its easier to see what you're using.

Implications importing java packages with wildcard

Absolutely no effect on run time.

Wildcard imports are probably marginally slower at compile-time, but I wouldn't think you'd need to care.

What package importing practice is better in Java?

If you want to follow some programming standard then do import specific e.g import java.util.Stack; We use sonarcube server which shows you potential exception that can occur as well programming standard that has been not maintained. It shows some time unused package import means you have imported some classes but you have not used it . So basically what you want to use import it & not other things.

Is it better to import specific packages or a whole tree with wildcards in java?

Also, you can't include a whole tree. You can cut down your import list to

import java.util.Calendar;
import java.util.logging.*;

but

import java.util.*;

does not import anything in the java.util.logging package. For the Java compiler, there are no subpackages. Also not for the VM (apart from some classloaders which use the package structure as a file directory structure).


Also, it seems you are mixing the concepts package and class/interface (or type). java.util and java.util.logging are packages, while java.util.Calendar, java.util.logging.Level etc. are classes.

A type (class/interface) is something you can use in your programm, while a package is mainly only a name space in which to put classes and interfaces. (Additionally, it has some consequences on visibility.)

You can import either single types (by specifying their name) or all types directly in a package (by specifying the package and .*). (You can also import all nested types in a type by using a wildcard, or import all static methods/fields of a class with import static <class>.*, just for completeness).

Importing is never recursive, wildcard importing is only for one level. (And you also can't use import java.util.*.* to import the logging classes.)

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

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.

Difference between import java.util.* and java.util.XXXX

If you include java.util.*, you're including all of the classes in the java.util package.

When including java.util.classname, you're only including the specified class in the java.util package.

Using java.util.* will not slow down the JVM because imports are handled at compile time, not runtime.



Related Topics



Leave a reply



Submit