Skip to content

Chapter 9 Java Standard Libraries

The Java programming language is accompanied by a vast set of standard libraries known as the Java API (Application Programming Interface). These libraries consist of classes, interfaces, and methods that provide fundamental capabilities and are readily accessible to all Java programs.

Overview of Java API

The Java API (Application Programming Interface) can be thought of as a collection of ready-made software components that provide a vast array of capabilities. It is a large collection of library routines that Java programs can call at will.

Java APIs include everything from collection data structures, to I/O routines, to networking protocols, to graphical user interface (GUI) building tools. These APIs are bundled together with the Java Development Kit (JDK).

Java APIs are grouped into libraries of related classes and interfaces. These libraries are known as packages. Here are some of the core packages that are used in many different types of Java programs:

  • java.lang: This package is automatically imported into every Java program. It contains classes that are fundamental to the design of the Java programming language. Key classes include Object, Class, System, String, Thread, Math, and many others.

  • java.util: This package contains the collections framework (which provides high-performance, high-quality implementations of useful data structures and algorithms), legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator, and a bit array).

  • java.io: This package provides for system input and output through data streams, serialization, and the file system. It provides classes for reading from and writing to byte streams and character streams.

  • java.net: This package provides the classes for implementing networking applications. It includes classes for implementing servers and clients of the TCP and UDP networking protocols and for the implementation of Internet Protocol (IP) networking.

  • java.math: This package provides classes for performing arbitrary-precision integer arithmetic (BigInteger) and arbitrary-precision decimal arithmetic (BigDecimal).

  • java.sql: This package provides the API for accessing and processing data stored in a data source (usually a relational database) using the Java programming language.

Remember, the Java API is extensive and covers a lot of ground. It's not necessary to memorize every package and class in the API. The best approach is to familiarize yourself with the general organization and capabilities of the API, and then learn the details of the parts that you actually use.

Commonly Used Classes and Methods

The Java API has numerous classes and methods, providing pre-built functionality for a wide variety of programming needs. Here are some commonly used classes and methods in more detail:

  1. java.lang.System: This class provides several useful class fields and methods. It cannot be instantiated which means you cannot create objects of this class. Instead, most of its methods are called through the class name itself. For example, System.out.println() is a frequently used method for printing to the console. Another commonly used feature of the System class is System.currentTimeMillis(), which returns the current time in milliseconds.

  2. java.lang.String: The String class represents character strings. All string literals, like "abc", are implemented as instances of this class. The String class includes several methods for examining the content of strings, finding characters or substrings within a string, changing case, and other tasks. For example, length(), charAt(int index), substring(int beginIndex, int endIndex), and split(String regex) are commonly used methods of the String class.

  3. java.lang.Math: The Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions. Notable methods include Math.sqrt(double a) for calculating the square root, Math.abs(int a) for obtaining the absolute value, and Math.random() for generating random numbers.

  4. java.util.Arrays: This class contains various methods for manipulating arrays, such as sorting and searching. Common methods include Arrays.sort(), which sorts an array into ascending numerical order, and Arrays.toString(array), which returns a string representation of the contents of the specified array.

  5. java.util.ArrayList: The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed, unlike standard arrays in Java. Key methods include add(E e), remove(int index), size(), and get(int index).

  6. java.util.HashMap: The HashMap class is a part of Java’s collection since Java 1.2. It provides the basic implementation of the Map interface of Java. It stores the data in (Key, Value) pairs. To access a value one must know its key. HashMap is known for its efficiency in retrieval operations.

It's important to know that the Java API is expansive and these are just a few of the most commonly used classes and methods. Over time, as you code more in Java, you'll naturally become more familiar with the API and discover new packages, classes, and methods that can make your programming tasks more efficient.