Java's Standard Packages
A package can contain both classes and other packages, and a package contained in another package is called a subpackage.
Why a Package Name Has Dots
Java's standard library is organized into packages, and the names of those packages often look longer than they first appear. The package that provides the Scanner class, for instance, is not called util on its own. It is called java.util. Before you can read a package name correctly, you need to know how one package can sit inside another. This article traces names from the outside in, so that by the end you can read a name such as java.io and say exactly where it lives.
Keep one idea in mind as you read: a package name is a path, not a flat label. Each dot in the name marks one step further into the structure, and the name records the chain of containers from the outermost one down to the one you are looking at.
Reading a Name From Left to Right
Take the name java.util. Reading from left to right, the leftmost part, java, is the outer package. The part after the dot, util, is a package that sits inside it. Reading the name from the inside outward would lose the relationship, so always read it from the outside in.
The same reading applies to java.io and java.net. Each has java on the left and a single additional segment on the right. In every case, the segment after the dot names a package that lives directly inside java. Neither name needs more than one dot, because each sits exactly one level below java in the structure.
The diagram above shows the reading order. Each dot moves you one step further into the containment chain. The Scanner class sits at the end of the name, after the package that holds it. The source's central point is that the dots record containment, so the name tells you where the package lives.
A Package That Holds Packages
A package can contain both classes and other packages. When a package contains another package, the inner one is called a subpackage. The container is the containing package, and the thing inside it is the subpackage.
This is the central fact of the article, and it changes how you should think about a package. A package is a container, and its contents are not limited to classes. The same package might hold ordinary classes directly, and also hold other packages. Each contained package is a subpackage of the one that holds it.
The source gives two top-level packages as examples of this structure. The java package and the javax package both contain subpackages. The java package has many of them. The source names java.util, java.io, and java.net, and it states that java.util is a subpackage of java. The diagram below places java at the top and shows the subpackages the source names directly beneath it.
Read the diagram from top to bottom and it matches the naming rule. Each child box carries the label of its parent followed by a dot. The java.util box holds the utility classes, and the source connects it to the Scanner class. The java.io box provides facilities for input and output, and the java.net box deals with network communication. The diagram shows only what the source describes. The java package contains more subpackages than these four, and the javax package also contains subpackages of its own that the source does not list.
Following the java Package's Contents
The source's own examples stay one level below java. The same method works at any depth, because the containment rule applies at every level. A subpackage can itself be a container for other packages. The procedure is always the same: start from the outermost package, then append each inner package with a dot.
Reconstructing a Deeper Package Name
Suppose util sits inside java, and a package called sub sits inside util. Build the full name of sub. The name sub is invented for illustration; the source does not describe a real Java package by this name.
- 1Start at the outermost package
The outermost package is java, so the name begins with java.
- 2Append the next level
util sits directly inside java, so the name becomes
java.util. - 3Append the innermost level
sub sits directly inside util, so the name becomes
java.util.sub. Each dot marks one more step into the structure.
java.util.sub
Notice what the worked example reveals. The name java.util.sub has two dots, so it sits two levels below the top. The name java.io has one dot, so it sits one level below java. Counting the dots and reading left to right gives you the depth and the chain of containers at once.
java.lang and Its Fundamental Classes
The source calls java.lang the most basic package. It contains fundamental classes, and the source lists four of them: String, Math, Integer, and Double. Many other parts of Java programming rely on these classes, which is why the source singles this package out as the foundation.
Its name still follows the containment pattern. java.lang is written with java on the left, so it is one of the packages held inside java, alongside java.util, java.io, and java.net. Being the most basic package does not place it outside the hierarchy. It sits at the same level as its sibling packages and is distinguished by the classes it holds.
Mistakes When Reading Package Names
Several errors come up often when people first read package names. Each one usually comes from treating a package name as a flat label rather than as a path through containers.
Common mistake Reading the name from the inside outward
What it looks likeReading java.util as util first, then java
Why it failsReading from the inside out loses the relationship that the name records, which is the chain of containers from the outermost package down.
Do this insteadRead from left to right. The leftmost part is the outer package, and each dot moves one step further in.
Common mistake Treating util as a complete package name
What it looks likeAssuming the package that provides Scanner is simply called util
Why it failsThe source states that the package providing Scanner is not called util on its own. The full name is
java.util.Do this insteadKeep the outer containers in the name. The full name records where the package sits.
Common mistake Assuming a dot always means a new top-level package
What it looks likeThinking java.io is a separate top-level package from java
Why it failsThe source says
java.iosits inside java. The dot records containment, not a new top-level package.Do this insteadRead java.io as a subpackage of java, one level below it.
Common mistake Assuming a name with more dots is unrelated to the names before it
Predicting the Outer Package
What do you think happens?
In the name java.util.sub, which part is the outermost package?
Reveal answer
java
The leftmost part of a full package name is the outermost package. In java.util.sub, java contains util, and util contains sub.
Without looking back, name the containing package of java.net and say whether java.net is a subpackage. Then explain in one sentence why java.lang belongs to the same hierarchy as java.io.
Hints
- Look at the segment to the left of the last dot.
- The source states that a package contained in another package is called a subpackage.
Key Takeaways
- A package can contain both classes and other packages, and a package contained in another package is called a subpackage.
- A full package name records the containment chain: in
java.util, util is contained within java, and you read the name from the outermost package inward. - To reconstruct a package name, start at the outermost package and append each inner package with a dot.
java.langis the most basic package and holds fundamental classes such as String, Math, Integer, and Double; its name places it inside java alongsidejava.util,java.io, andjava.net.