The Java Standard Library Overview
A package groups related classes, and it can also contain other packages, which gives Java a way to organize code at several levels.
Why Java Sorts Classes into Packages
Java ships with a very large collection of ready-made classes. Each class offers tools for a specific job, such as reading user input, doing arithmetic, or communicating over a network. If every one of those classes sat in a single undivided list, finding the right one would be slow and confusing. Java solves this problem with packages, which give the standard library a shape that a programmer can navigate.
As you read this article, keep one question in mind: when you see a name such as java.util, what does each part of that name tell you about where a class lives? By the end you should be able to answer that question for any standard package you meet.
A Package as a Labeled Container
Think of a package as a labeled container that holds related material. Its job is to keep things that belong together in one place. Inside a package you will find classes, and a package may also hold other packages. That second possibility is what lets Java organize code at more than one level of detail.
The container analogy has a limit worth noticing. In Java, a package is a grouping of names and classes, and it does not perform any work by itself. Its value is organizational. It tells you which classes belong together and gives each class a complete name that is unambiguous, so two classes with the same short name can live in different packages without being confused.
The diagram shows the same nesting as a family tree. A package can hold classes at its own level and also hold subpackages, which are packages one level down. The java package holds several subpackages, and util is one of them. Scanner sits inside util, so it is contained at two levels: inside java.util, which is itself inside java.
Reading a Full Name from the Outside In
A subpackage is a package contained in another package, and its full name includes every parent package. The java package holds a subpackage called util, which contains utility classes, including the Scanner class you have probably already used to read input. Because util sits inside java, its name is not just util. Its full name is java.util, with the parent name written first and a dot separating each level.
- 1Start with the top-level package at the left of the name, which here is java.
- 2Move across the first dot to find the subpackage that sits directly inside java, which is util.
- 3Keep following dots while the names are still packages. Each dot marks a boundary between a container and something it contains.
- 4Read the final segment as the class. In
java.util.Scanner, the last segment Scanner is the class, and everything before it is the chain of packages that holds it.
Building the Full Name of a Subpackage
Write the full name of the io subpackage, which sits inside java.
- 1Identify the parent
io is contained in java, so java is its parent.
- 2Write the parent first
Place the parent name before the subpackage name: java then io.
- 3Insert a dot
A dot separates each level, giving
java.io.
The full name is java.io, not io and not util.io.
The java and javax Top-Level Packages
The entire standard Java API is spread across a few top-level packages, and two of them matter most at the start. The package named java is the largest of the group. It contains several non-GUI packages, along with the original AWT graphical user interface classes. The package named javax also appears in the standard library. It holds the classes used by the Swing graphical user interface, along with many other classes. Both java and javax contain subpackages.
| Top-level package | What it holds | Example contents |
|---|---|---|
| java | Fundamental and non-GUI packages, plus the original AWT GUI classes | java.lang, java.util, java.io, java.net |
| javax | Swing GUI classes and many other classes | Swing graphical user interface classes |
The two top-level packages in the standard library, as described in the source material
Inside java: lang, util, io, and net
Several of the most useful packages live inside java. Each one groups classes for a different kind of job. The sections below describe them in turn, and the comparison that follows puts them side by side.
Mistakes When Reading or Writing Package Names
Common mistake Writing only the last part of a subpackage name
What it looks likeWriting util.Scanner instead of java.util.Scanner
Why it failsA subpackage's full name includes every parent package, so util on its own does not identify where the class lives.
Do this insteadWrite the parent first and separate each level with a dot: java.util.Scanner.
Common mistake Treating sibling subpackages as nested inside each other
What it looks likeWriting java.util.io for the input and output package
Why it failsio and util are both contained in java. They are siblings, so neither sits inside the other.
Do this insteadWrite java.io, with java as the only parent.
Common mistake Confusing java with javax
What it looks likeAssuming Swing classes live in java because the names look alike
Why it failsThe javax package holds the Swing GUI classes, while java holds the fundamental and non-GUI packages along with the original AWT classes.
Do this insteadCheck which top-level package a class belongs to before assuming its location from a similar-looking name.
Common mistake Expecting java.lang to need an import
What it looks likeAdding an import line before using String in every program
Why it failsjava.langholds the classes Java programs use most often, and you rarely need an import statement for them.Do this insteadUse String, Math, Integer, and Double directly, and reserve imports for packages such as java.util.
Common mistake Treating a dot as part of the class name rather than a boundary between containers
Practice Reading Package Names
Without looking back, write the full name of a class called Scanner that lives inside the util subpackage of java. Then say which part of that name is the top-level package and which part is the class.
Hints
- The full name starts with the top-level package and includes every parent.
- Separate each level with a dot, and the last segment is the class.
What do you think happens?
Which package is the top-level package in the full name java.util.Scanner?
Reveal answer
java
java is the outermost container. util is a subpackage inside java, and Scanner is the class at the end of the chain.
Given the subpackages io and net, both inside java, write their full names. Then explain why java.util.io is incorrect.
Hints
- Both io and net sit directly inside java, not inside util.
- A subpackage's full name lists its parents in order from the top.
Key Takeaways
- A package is a labeled container that groups related classes and can also hold other packages, which lets Java organize code at several levels.
- A subpackage's full name includes every parent package, separated by dots, so
java.utilis the util subpackage inside java and Scanner is the class at the end ofjava.util.Scanner. - The java package holds fundamental and non-GUI packages along with the original AWT classes, javax holds Swing GUI classes and many others, and javafx is not a standard part of Java.
java.langholds fundamental classes such as String, Math, Integer, and Double,java.utilholds utility classes such as Scanner, andjava.ioandjava.netare sibling subpackages of java.- Read full names from the outside in and avoid writing only the last segment, treating siblings as nested, or confusing java with javax.