Using the Scanner Class
A package groups related classes, and it can also contain other packages, which gives Java a way to organize code at several levels.
Why a Huge Library Needs Shelves
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 library a shape that a programmer can navigate.
Keep one question in mind as you read this article: 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. You have probably already used a class from java.util without thinking about its full name, so that is a good place to start.
What do you think happens?
Before reading on: is the full name of the util package just util, or is it written with its parent package in front?
Reveal answer
java.util, with the parent name written first
A subpackage's full name includes every parent package. Because util sits inside java, its full name is java.util, with a dot separating each level.
What a Package Holds
Think of a package as a labeled container that holds related material. The container's 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.
Tracing the Levels of Containment
The clearest way to see nesting is to follow it level by level. The java package is the top of one branch. It holds several subpackages of its own, and it also contains several non-GUI packages along with the original AWT graphical user interface classes. The package named javax is a separate top-level package. It holds the classes used by the Swing graphical user interface, along with many other classes. Both java and javax contain subpackages.
Read the tree downward to find what a package contains, and read it upward to find a subpackage's parent. The diagram shows java as the parent of lang, util, io, and net. The javax package sits at the same top level as java, and the source describes it as holding Swing classes and more without listing its subpackages in detail.
Building a Full Name from Parent Packages
A subpackage is a package contained in another package, and its full name includes every parent package. The util subpackage sits inside java, so its full name is java.util, with the parent name written first and a dot separating each level. The dot marks the boundary between a container and something it contains.
Building the full name of a class inside a subpackage
A subpackage named util sits inside java, and it contains a class named Scanner. What is the full name of that class?
- 1Start with the top-level package
The outermost container is java, so the name starts with java.
- 2Add the subpackage
util sits inside java, so it is written after java with a dot between them, giving
java.util. - 3Add the class
Scanner lives inside util, so it is written last with a dot, giving
java.util.Scanner. - 4Check the direction
Building the name reads from the inside out: you start with the innermost container and add each parent in front of it.
The full name is java.util.Scanner. The last segment is the class, and everything before it is the chain of packages that holds it.
The process reverses when you take a name apart. Reading from the outside in, you move through each dot: java contains util, and util contains Scanner. Each dot marks one boundary, so a name with more dots describes a deeper level of nesting.
Siblings Inside java
The subpackages io and net also live inside java. They are siblings of util, not children of it. Sibling subpackages such as java.io and java.util are both contained in java. This is why their full names are java.io and java.net rather than java.util.io or anything similar. A subpackage's name always reflects its actual parent, not the sibling beside it.
The source describes util as holding utility classes, including Scanner, and describes network communication as one of the jobs a class can perform. The names io and net suggest input-output and networking work, which fits the examples the source gives of tasks classes handle. The source itself does not list every class inside io or net, so treat those descriptions as guides to the purpose the name signals.
The java.lang and javax Packages
Within java, the most basic package is java.lang. It contains fundamental classes such as String, Math, Integer, and Double. Because these are the classes that Java programs use most often, you rarely need an import statement for them. The javax package is different: it is the home of the Swing graphical user interface classes, and it is not a subpackage of java.
| Package | Position in hierarchy | Purpose from the source |
|---|---|---|
| java | Top-level package | Holds fundamental and non-GUI packages, plus the original AWT GUI classes |
| javax | Top-level package | Holds Swing GUI classes and many other classes |
| java.lang | Subpackage of java | Fundamental classes such as String, Math, Integer, and Double |
| java.util | Subpackage of java | Utility classes, including Scanner |
| java.io | Subpackage of java | Sibling of java.util; the source does not list its classes |
| java.net | Subpackage of java | Sibling of java.util; network communication work |
Where each package named in this article sits and what the source says it holds
The source notes that javafx is not a standard part of Java. When you meet a name that starts with javafx, do not assume it belongs to the standard library just because it looks similar to java or javax.
Mistakes When Reading Package Names
Common mistake Writing only the last segment of a subpackage name
What it looks likeCalling the util subpackage just util
Why it failsA subpackage's full name includes every parent package, so util alone does not say where the package lives.
Do this insteadWrite java.util, with the parent name first and a dot between each level.
Common mistake Assuming a sibling is a child
What it looks likeWriting java.util.io for the input-output package
Why it failsio and net are siblings of util. Both sit directly inside java.
Do this insteadWrite java.io, with java as the only parent.
Common mistake Forgetting the dot between levels
What it looks likeWriting javautil instead of java.util
Why it failsEach dot marks the boundary between a container and something it contains. Without the dot, the name no longer shows the nesting.
Do this insteadPlace a dot between every pair of adjacent levels.
Common mistake Treating javax as a subpackage of java
What it looks likeAssuming Swing classes live under java.swing
Why it failsjavax is a separate top-level package that holds Swing GUI classes.
Do this insteadStart the name from javax, not from java.
Common mistake Assuming any javafx name is standard
What it looks likeTreating javafx as built into every Java installation
Why it failsThe source states that javafx is not a standard part of Java.
Do this insteadCheck whether a package is part of the standard library before relying on it.
Practice Reading and Writing Names
Without looking back, write the full name of a class named Scanner that sits in the util subpackage, which sits in java. Then say which package is the parent of io and which package is the sibling of util.
Hints
- Start from the outermost container and add each level with a dot.
- Siblings share the same parent, so io and util both have java as their parent.
A friend writes the name of the input-output subpackage as java.util.io. Explain in one or two sentences why this is wrong and write the correct full name.
Hints
- Ask which package io is contained in, and which package it is a sibling of.
- The correct full name keeps java as the only parent.
Summary of Package Organization
- 1A package groups related classes, and it can also contain other packages, which lets Java organize code at several levels.
- 2A subpackage's full name includes every parent package, separated by dots, such as
java.util. - 3java holds fundamental and non-GUI packages, javax holds Swing GUI classes and more, and
java.langholds String, Math, Integer, and Double. - 4Sibling subpackages such as
java.io,java.util, andjava.netshare the parent java and are not nested inside one another.
At a glance
- A package groups related classes and can also contain other packages, giving Java a way to organize code at several levels.
- A subpackage's full name includes every parent package, with dots marking each boundary, such as
java.util. - java and javax are top-level packages;
java.lang,java.util,java.io, andjava.netare subpackages inside java. - Sibling subpackages share a parent, so their names reflect that parent rather than each other.