The API (Application Programming Interface) provides all of the classes that are included as part of the JDK (Java Development Kit).
In your study of Java, you will utilize many of the different Java classes.
The Java SE API stores classes in packages. We will be using three of the most common packages in this unit.
1. The java.lang package contains the classes that are the most fundamental to the Java language. As a result, this package is automatically available to all Java code and requires no action (no importing) on the part of the programmer. The String class, for example, is in this package.
2. The java.util package contains utility classes, such as those needed to obtain input from the user through the console (keyboard). This package needs to be imported to access its classes. We will be using the "Scanner class" in this package to obtain keyboard data from the user.
import java.util.Scanner;
3. The java.text package contains classes that deal with text manipulation, including those for formatting numbers, dates and amounts of money. This package needs to be imported to access its classes. We will be using the "DecimalFormat class" in this package to control the output of decimal entries, including money.
import java.text.DecimalFormat;
or import java.text.*;
When you import a package, you can import ONLY the class you are interested in using by specifying its name, or you can import ALL of the classes in the package by using an asterisk (*) in place of a specific class name. You will see us using both approaches in the next few lessons.
While it may seem easier to always import ALL of the classes (just to cover all of your bases), it can cause problems. For example, a "Date class" exists in more than one package. If you should import all classes from both of these packages, the computer would not know which Date class it should be using. |