8.1 System Libraries

This section under major construction.

Java includes a vast number of libraries that have been designed and implemented by experts. Even more general-purpose libraries are available from the Web for download. Many of the data structures and algorithms we have considered already (binary heaps, binary search trees, hash tables, quicksort) play a central role in these libraries. Avoid re-inventing wheel. Knowing how to find the right library and interface with it is a useful skill.

Here are some classes that are worth knowing about.

NumberFormatter. DecimalFormat formats real numbers for printing with System.out.println.

DecimalFormat df = new DecimalFormat("###.##");

BigInteger. This class implements arithmetic operations on arbitrary precision integers, including addition, multiplication, modular arithmetic, primality testing, prime number generation, and bit manipulations. This class is useful for implementing various cryptographic schemes including RSA.

StringBuffer. There are two built-in classes for manipulating strings: String and StringBuffer. The first is used to store an immutable sequence of characters, i.e., a string whose value will never change. The second is to store a string whose characters may be modified. Behind the scenes, the compiler uses a StringBuffer to perform any concatenations operations, e.g., s = "Hello " + "World".

A common performance bug is to concatenate a sequence of characters (or words) together, one at a time, to form a long string. The following code illustrates the problem.

String s = "";
while(!In.isEmpty()) {
   char c = In.getChar();
   s = s + c;
A problem arises because each time the new character c is appended to s, a new copy of s is created since strings are immutable. It can take time proportional to N^2 to construct a string of length N. Instead, you should use a StringBuffer so that the time is proportional to N.
StringBuffer sb = new StringBuffer();
while(!In.isEmpty()) {
   char c = In.getChar();
String s = sb.toString();

Packages. There are many times when no available library fits your needs. In this case, you must create your own library. In Java, there is a built-in mechanism called a package that organizes related classes so that it can be accessed just like the built-in system libraries. To build a package named Jama, you must include the statement import Jama; at the beginning of the client program. The classes in the Jama package must be stored in a subdirectory named Jama. The parent directory must be in your CLASSPATH, e.g., the current directory.

Javadoc. Run Javadoc on the Jama package to automatically produce Java style documentation. The command

% javadoc -d Jama/javadoc Jama
produces a file Jama/javadoc/index.html that documents all of the classes and methods in the Jama package.

HTML parsing. There are many important libraries outside the ones built directly in to Java. For example, if you want to parse web documents in HTML, you might want to use HTMLParser v 1.3. Example applications: web crawling, extracting text content. Explain how to download an external library as a .jar file and use it.


Creative Exercises


  1. Use built-in libraries to save time in coding.

  2. Pay attention to performance guarantees (or lack thereof) in system libraries.

  3. Use packages to build your own libraries.

  4. Use javadoc to automatically generate documentation. libraries.