Java Reference
In-Depth Information
To make use of the existing classes, methods, and identifiers, you must tell the program
which package contains the appropriate information. The
import
statement helps you do this.
The general syntax to import the contents of a package in a Java program is:
import
packageName.*;
In Java,
import
is a reserved word. For example, the following statement imports the
necessary classes from the package
java.util
:
import
java.util.*;
To import a specific class from a package, you can specify the name of the class in
place of the
*
. The following statement imports the
class
Scanner
from the package
java.util
:
import
java.util.Scanner;
Import statements are placed at the top of the program.
If you use the character
*
in the
import
statement, as in the statement:
import
java.util.*;
then the compiler determines the relevant class(es) used in the program.
The primitive data types are directly part of the Java language and do not require that any
package be imported into the program. Also, the
class
String
is contained in the
package
java.lang
. You do not need to import classes from the package
java.lang
.
The system automatically does it for you.
In previous sections, you learned enough Java concepts to write meaningful programs. In
this section, you will learn how to create a complete Java application program.
The basic unit of a Java program is called a class. A Java application program is, therefore, a
collection of one or more classes. Roughly speaking, a class is a collection of methods and
data members. As described in the previous section, a method is a set of instructions
designed to accomplish a specific task. Some predefined or standard methods, such as
nextInt
,
print
, and
println
, are already written and are provided as part of the system.
But to accomplish most tasks, programmers must learn to write their own methods.
One of the classes in a Java application program must have the method called
main
.
Moreover, there can only be one method
main
in a Java class. If a Java application
program has only one class, it must contain the method
main
. Until Chapter 6, other than
Search WWH ::
Custom Search