Wednesday 18 May 2022

Wildcard Imports and its Performance effect

 



Here we will discuss about
  • Wildcard Imports
  • How wildcard imports affect performance 
  • How to disable wildcraft imports in IntelliJ

Java developers use the wildcard (*) in import statements to import all the classes in a particular package. But in code reviews, most of you may have asked to remove those wildcard imports and add the actual class name.

Is it really matter having wildcards in import statements in Java?

Purpose of this article is to discuss the above problem. I’m approaching it in 2 ways,

  • Performance
  • Clean code

What is Import Statements
Java import statement is a syntactical sugar. Import statement tells the compiler where to find the names ( Classes, static methods, etc.) used in the source code. Which means, if you use the fully qualified names in the source code, then you don’t need import statements💡
    

package com.example;
import java.util.ArrayList;
import java.util.List;
public class WithImport {
public static void main(String[] args) {
List myList = new ArrayList();
}
}
Example 1 — Code with import statements

In the above code block, I’ve used List and ArrayList classes from thejava.util package. We can write the same code without import statements as bellow.

package com.example; public class WithoutImport {
public static void main(String[] args) {
java.util.List myList = new java.util.ArrayList();
}
}

In the above example, I’ve used fully qualified names for List and ArrayListclasses. If we compile and run these codes, the output would be the same.

Then what happens with the wildcard import?

Wildcard imports tell java compiler to search for class names in the given package. Hence, using wild card imports, the compile-time performance may lower a bit. But it won’t be a notable impact in most cases. In run time, there’s no performance issue at all because these import statements are compiler directives and we can’t find them in the byte code. To clarify this we’ll write the above example with wildcard imports.

package com.example; import java.util.*;
public class WithWildCard {
public static void main(String[] args) {
List myList = new ArrayList();
}
}
Example 3 — Code with wildcard imports

If we get the byte code of this class, it would look like following


Compiled from "WithWildCard.java" public class com.example.WithWildCard { public com.example.WithWildCard();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: new #2 // class java/util/ArrayList
3: dup
4: invokespecial #3 // Method java/util/ArrayList."<init>":()V
7: astore_1
8: return
}

IF we compare it with the byte code of example 1 (Code with import statements), both have the same byte code. In the runtime, Java uses the byte code, not the source code. In the byte code, there’s no import statements. That clearly implies that using wildcard imports does not affect the runtime performance of a Java application.


Coding best practices

The main drawback of using wildcard imports in Java is possible naming conflicts. Let’s assume that we have developed an ArrayList class ourself which implements the java.util.List interface. But if we use the import statement import java.util.* as in example 3, Compiler would pickup the class java.util.ArrayList instead of our implementation. And sometimes, if we import using the wildcard and the developer of that imported package adds a new class or rename one to a class that we are already using in our code, then the code won’t compile due to class name conflicts.

Apart from the class name resolving issue, code readability would drop with the wildcard import since the developers don't get a clear idea of the actual classes using by the code. But if you are using a large number of resources from a package, using the wildcard looks cleaner sometimes.

How to disable wildcraft imports in IntelliJs

To force IntelliJ to include each and every import individually.

Go to Preferences ( + , on macOS / Ctrl + Alt + S on Windows and Linux) > Editor > Code Style > Java > Imports tab set Class count to use import with '*' and Names count to use static import with '*' to a higher value. Any value over 99 seems to work fine.

Skill is Vision

Author & Editor

It is possible to fly without motors, but not without knowledge and skill. The future belongs to those who learn more skills and combine them in creative ways.

0 comments:

Post a Comment