Concepts / Using the Scanner Class

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.

  • Programming

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?

  • Just util

  • java.util, with the parent name written first

Reveal answer
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.

javafundamental and non-GUIpackagesjava.langString, Math, Integer,DoublejavaxSwing GUI classes and morejava.utilutility classes includingScannerjava.iojava.netcontainscontainscontainscontains
What does each package contain, and how do the java, javax, and java.lang levels nest inside one another? Read downward to find what a package contains.

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.

javatop-level package.boundary: container tocontentsutilsubpackage inside java.boundary: package to classScannerclass inside util
How do the parent package names combine with dots to form the full name of a subpackage such as java.util, and where does the class name fit at the end?
Worked example

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?

  1. 1
    Start with the top-level package

    The outermost container is java, so the name starts with java.

  2. 2
    Add the subpackage

    util sits inside java, so it is written after java with a dot between them, giving java.util.

  3. 3
    Add the class

    Scanner lives inside util, so it is written last with a dot, giving java.util.Scanner.

  4. 4
    Check the direction

    Building the name reads from the inside out: you start with the innermost container and add each parent in front of it.

Result

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.

javashared parent packagejava.utilutility classes such asScannerjava.ioinput and output classesjava.netnetwork communicationclassescontainscontainscontains
Which subpackage sits under java, and how do java.util, java.io, and java.net differ in purpose? The three share a parent but hold different kinds of classes.

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.

PackagePosition in hierarchyPurpose from the source
javaTop-level packageHolds fundamental and non-GUI packages, plus the original AWT GUI classes
javaxTop-level packageHolds Swing GUI classes and many other classes
java.langSubpackage of javaFundamental classes such as String, Math, Integer, and Double
java.utilSubpackage of javaUtility classes, including Scanner
java.ioSubpackage of javaSibling of java.util; the source does not list its classes
java.netSubpackage of javaSibling 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 like

    Calling the util subpackage just util

    Why it fails

    A subpackage's full name includes every parent package, so util alone does not say where the package lives.

    Do this instead

    Write java.util, with the parent name first and a dot between each level.

  • Common mistake

    Assuming a sibling is a child

    What it looks like

    Writing java.util.io for the input-output package

    Why it fails

    io and net are siblings of util. Both sit directly inside java.

    Do this instead

    Write java.io, with java as the only parent.

  • Common mistake

    Forgetting the dot between levels

    What it looks like

    Writing javautil instead of java.util

    Why it fails

    Each dot marks the boundary between a container and something it contains. Without the dot, the name no longer shows the nesting.

    Do this instead

    Place a dot between every pair of adjacent levels.

  • Common mistake

    Treating javax as a subpackage of java

    What it looks like

    Assuming Swing classes live under java.swing

    Why it fails

    javax is a separate top-level package that holds Swing GUI classes.

    Do this instead

    Start the name from javax, not from java.

  • Common mistake

    Assuming any javafx name is standard

    What it looks like

    Treating javafx as built into every Java installation

    Why it fails

    The source states that javafx is not a standard part of Java.

    Do this instead

    Check whether a package is part of the standard library before relying on it.

Practice Reading and Writing Names

EASY

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.
MEDIUM

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

  1. 1
    A package groups related classes, and it can also contain other packages, which lets Java organize code at several levels.
  2. 2
    A subpackage's full name includes every parent package, separated by dots, such as java.util.
  3. 3
    java holds fundamental and non-GUI packages, javax holds Swing GUI classes and more, and java.lang holds String, Math, Integer, and Double.
  4. 4
    Sibling subpackages such as java.io, java.util, and java.net share 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, and java.net are subpackages inside java.
  • Sibling subpackages share a parent, so their names reflect that parent rather than each other.