A package is a named group of related classes and interfaces. It is Java's way of organising code, much as folders organise files.

Basics of Packages

A package is a collection of related classes, interfaces and sub-packages, grouped under one name. Packages give four benefits. They organise classes into groups, so that related classes are easy to find. They prevent name conflicts, since two classes with the same name can exist in different packages, such as java.util.Date and java.sql.Date. They provide access protection, since some members can be visible only inside their package. And they support reuse, since a package can be used by many programs. A package name corresponds to a directory on disk: the class mypack.Book is stored as mypack/Book.class.

Definition: Package. A named group of related classes, interfaces and sub-packages that organises code, prevents name conflicts and controls access.

System Packages

Java's standard library, the API, is organised into system (built-in) packages. java.lang contains the basic classes, such as String, Math, System, Object, Thread and the wrapper classes; it is imported automatically into every program. java.util contains utility classes, such as Scanner, Vector, ArrayList, HashMap, Arrays, Random and Date. java.io contains classes for input and output, such as File, FileReader and BufferedReader. java.net contains networking classes, such as URL and Socket. java.awt and javax.swing contain classes for graphical user interfaces, and java.sql contains classes for databases.

Using System Packages

A class from a package can be used in two ways. By its fully qualified name: java.util.Scanner sc = new java.util.Scanner(System.in);. Or by importing it at the top of the file: import java.util.Scanner; imports one class, and *import java.util.; imports all the classes of the package (but not its sub-packages). Importing does not make a program larger; it only lets the compiler find the class by its short name. Static import* brings in static members: import static java.lang.Math.; allows sqrt(x) instead of Math.sqrt(x).

Table 4.1: Common system packages

Package Contains Examples
java.lang Basic language classes (auto-imported) String, Math, System
java.util Utilities and collections Scanner, Vector, Arrays
java.io Input and output File, BufferedReader
java.net Networking URL, Socket
java.awt, javax.swing Graphical interfaces Frame, JButton

Example 4.1: Using classes from system packages

Problem: Write a program that uses java.util.Arrays to sort numbers, java.util.Random with a fixed seed to make numbers, and a static import of Math.

Solution:

  1. Import java.util.Arrays and java.util.Random.
  2. Use import static java.lang.Math.sqrt to call sqrt directly.
  3. Create the numbers, sort them and print them with their square roots.

Result: The program shows that classes from different packages work together through imports.

import java.util.Arrays;
import java.util.Random;
import static java.lang.Math.sqrt;
public class PackageUse {
    public static void main(String[] args) {
        Random r = new Random(7);           // fixed seed
        int[] a = new int[5];
        for (int i = 0; i < a.length; i++) a[i] = r.nextInt(100);
        Arrays.sort(a);
        System.out.println("sorted: " + Arrays.toString(a));
        System.out.printf("sqrt(%d) = %.2f%n", a[4], sqrt(a[4]));
        java.util.Date d = new java.util.Date(0);
        // fully qualified name
        System.out.println("class of d: " + d.getClass().getName());
    }
}

Output

sorted: [36, 44, 64, 80, 85]
sqrt(85) = 9.22
class of d: java.util.Date

Common mistakes

  • Thinking that import java.util.*; also imports java.util.function or other sub-packages.
  • Importing java.lang, which is already imported.
  • Using a class from another package without importing it or giving its full name.

Key takeaways

  • A package groups related classes and maps to a directory.
  • Packages organise code, prevent name clashes and control access.
  • java.lang is imported automatically.
  • Use a fully qualified name or an import statement.

Practice questions

  1. What is a package?
  2. Which package is imported automatically?
  3. What does import java.util.*; do?

Answers to the practice questions

  1. A named group of related classes and interfaces that organises code and prevents name conflicts.
  2. java.lang.
  3. It allows all classes of java.util to be used by their short names.

Sources

  • Official BCA syllabus Object Oriented Programming using Java, course code 25BCA402SEC01, Unit IV