Yes. For most of the classes that come with Java you need to tell the compiler where they are found.
Here is a program that creates a Scanner object.
The program does nothing with this object (the next chapter will do that).
public class ImportDemo02
{
public static void main ( String[] args )
{
java.util.Scanner scan ;
scan = new java.util.Scanner( System.in );
// do something with the Scanner object (see next chapter)
}
}
The compiler is told that the class Scanner is in the package java.util.
Another way to do this is to import the class, as in the following program:
import java.util.Scanner; public class ImportDemo03 { public static void main ( String[] args ) { Scanner scan ; scan = new Scanner( System.in ); // do something with the Scanner object (see next chapter) } }
In the second version of the program, the statement:
import java.util.Scanner;
tells the compiler that the Scanner class is found
in the java.util package, so
the rest of the program does not need to include the package name each
time Scanner is used.
The package java.util contains various utility classes.
Might a program need more than one of them?