Packages & Modules
import Statement
Master-type imports vs wildcard imports, compile-time symbol resolutions, and wildcard namespace collisions.
Interview: Evaluates wildcard import compile-time resolution mechanics, runtime footprint impacts, and collision resolution techniques.
The import statement tells the compiler where to look for class names that are not fully qualified. While developers use both explicit and wildcard imports, resolving class name collisions is essential.
Compile Resolution
Wildcard imports (e.g. *) do not increase runtime memory size or impact app execution speed; they are resolved at compile time.
Namespace Conflict
If two wildcards import identical class names (e.g. Date), the compiler flags it as ambiguous, forcing explicit resolution.
Default imports
The package java.lang.* is automatically imported by the compiler into every Java source file.
Wildcard Collision Resolution Rules
When using wildcard imports (like import java.util.*; and import java.sql.*;), duplicate class definitions can conflict:
- Compiler Ambiguity: If you reference
Date, the compiler does not know whether to loadjava.util.Dateorjava.sql.Date, causing a compile-time error. - Explicit Resolution: You must resolve this by adding an explicit, single-type import for the preferred class (e.g.,
import java.util.Date;). Explicit imports take precedence over wildcard imports. - Fully Qualified Names: Alternatively, refer to the classes by their fully qualified names directly in the code (e.g.,
java.sql.Date sqlDate = new java.sql.Date(...)).
Common Pitfalls
- Indiscriminate Wildcard Imports: Importing whole packages containing hundreds of classes, increasing the likelihood of name collisions when new libraries are added.
- Confusing Sub-Package imports: Assuming
import java.util.*;also imports classes injava.util.concurrent.*. Wildcards do not match recursively.
Best Practices
- Prefer Explicit imports: List each imported class explicitly. This documents dependencies clearly and avoids name collisions.
- Utilize IDE Auto-Optimization: Set up your IDE to optimize imports automatically, removing unused imports and formatting the list.
Interview-Relevant Information
Q1: Do wildcard imports impact application performance or runtime memory size?
Answer: No. The import statement is a compile-time directive that tells the compiler where to resolve class names. The compiler replaces all shortened names with fully qualified class names in the generated bytecode. As a result, the compiled .class files are identical whether you use explicit or wildcard imports.
Q2: If an explicit import and a wildcard import conflict, which one takes precedence?
Answer: The explicit import takes precedence. If you declare import java.util.Date; and also have import java.sql.*;, the compiler resolves references to Date as java.util.Date.
Quick Checklist
Can you explain compile-time import resolution, resolve class name collisions using explicit imports, and explain why sub-packages are not imported recursively by wildcards? If yes, you understand import statements.
Use Cases
Importing utility classes to make code concise and readable.
Handling namespaces when dealing with overlapping classes (e.g. org.w3c.dom vs org.jdom2).
Common Mistakes
Expecting wildcard imports to import sub-packages recursively.
Leaving unused imports in source files, which increases compilation times slightly and clutters code reviews.