Import Statements & Packages in Java
A package is Java's namespacing mechanism, grouping related classes together and preventing naming collisions between classes that might otherwise share a name — java.util.List and some hypothetical com.example.List can coexist without conflict, since they live in different packages. An import statement, import java.util.List;, lets code refer to a class by its simple name, List, instead of writing its fully-qualified name every single time it's used. Imports are purely a convenience for the source code's readability, resolved entirely at compile time, with zero effect on the compiled program's actual runtime behavior.
import java.util.*; imports every class directly inside java.util at once, though it does not reach into further sub-packages. Classes inside java.lang — String, Integer, Math, and similar extremely common types — are imported automatically into every single Java file, which is why they never need an explicit import statement at all.
What a package is
A namespace grouping related classes together, preventing naming collisions between classes that might otherwise share a name.
A fully-qualified name
java.util.List names a class unambiguously by including its full package path.
import for convenience
import java.util.List; lets the rest of the file refer to it as just List afterward.
Wildcard imports
import java.util.*; imports every class directly inside that package at once, though not any further sub-packages.
java.lang is automatic
String, Integer, Math, and the rest of java.lang are available in every file with no import statement needed at all.
import java.util.List; public class ImportDemo { public static void main(String[] args) { List<String> names = List.of("Ana", "Bo"); System.out.println(names); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.