Concepts / The Java Standard Library Overview

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.

  • Programming

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.

javatop-level packagejava.langfundamental classesScannerclass inside java.utiljavaxtop-level packagejava.utilutility classesjava.iosubpackage of javajava.netsubpackage of javacontainscontainscontainscontainscontains
What does a package contain, and how do classes and subpackages nest inside one another? Read downward to see what a package contains and upward to find a subpackage's parent.

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.

  1. 1
    Start with the top-level package at the left of the name, which here is java.
  2. 2
    Move across the first dot to find the subpackage that sits directly inside java, which is util.
  3. 3
    Keep following dots while the names are still packages. Each dot marks a boundary between a container and something it contains.
  4. 4
    Read 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.
javatop-level package.container boundaryutilsubpackage inside java.container boundaryScannerclass, the last segmentparent ofcontains
Which part of the name identifies which level? Each token marks one level of the nesting, and each dot marks a container boundary.
Worked example

Building the Full Name of a Subpackage

Write the full name of the io subpackage, which sits inside java.

  1. 1
    Identify the parent

    io is contained in java, so java is its parent.

  2. 2
    Write the parent first

    Place the parent name before the subpackage name: java then io.

  3. 3
    Insert a dot

    A dot separates each level, giving java.io.

Result

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 packageWhat it holdsExample contents
javaFundamental and non-GUI packages, plus the original AWT GUI classesjava.lang, java.util, java.io, java.net
javaxSwing GUI classes and many other classesSwing 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 like

    Writing util.Scanner instead of java.util.Scanner

    Why it fails

    A subpackage's full name includes every parent package, so util on its own does not identify where the class lives.

    Do this instead

    Write 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 like

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

    Why it fails

    io and util are both contained in java. They are siblings, so neither sits inside the other.

    Do this instead

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

  • Common mistake

    Confusing java with javax

    What it looks like

    Assuming Swing classes live in java because the names look alike

    Why it fails

    The javax package holds the Swing GUI classes, while java holds the fundamental and non-GUI packages along with the original AWT classes.

    Do this instead

    Check 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 like

    Adding an import line before using String in every program

    Why it fails

    java.lang holds the classes Java programs use most often, and you rarely need an import statement for them.

    Do this instead

    Use 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

EASY

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?

  • java

  • util

  • Scanner

Reveal answer
Answer:

java

java is the outermost container. util is a subpackage inside java, and Scanner is the class at the end of the chain.

MEDIUM

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.util is the util subpackage inside java and Scanner is the class at the end of java.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.lang holds fundamental classes such as String, Math, Integer, and Double, java.util holds utility classes such as Scanner, and java.io and java.net are 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.