Learn Java
import com.learnjava.packagesstandardlibrary.ImportPackages;

Import Statements & Packages in Java

@Concept

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.

@HowItWorks
1

What a package is

A namespace grouping related classes together, preventing naming collisions between classes that might otherwise share a name.

2

A fully-qualified name

java.util.List names a class unambiguously by including its full package path.

3

import for convenience

import java.util.List; lets the rest of the file refer to it as just List afterward.

4

Wildcard imports

import java.util.*; imports every class directly inside that package at once, though not any further sub-packages.

5

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.

@Example
import java.util.List;
 
public class ImportDemo {
public static void main(String[] args) {
List<String> names = List.of("Ana", "Bo");
System.out.println(names);
}
}
Output
[Ana, Bo]
1import java.util.List; lets the rest of this file refer to the class as just List, instead of java.util.List every time.
2List.of("Ana", "Bo") creates a small, fixed list using that imported simple name.
3String, used implicitly for each list element, needs no import at all, since it lives in java.lang.
4The single println call prints the list's own toString() representation as one line.
@UseCases
Referring to a frequently-used class by its short name instead of its full package path throughout a file
Understanding why two classes with the same simple name, from different packages, don't conflict
Recognizing that String, Integer, and Math never need an explicit import
Reading an unfamiliar file's import list to quickly see which external classes and packages it actually depends on

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.