Java Reference
In-Depth Information
classes
—and are collectively referred to as the
Java class library
, or the
Java Application
Programming Interface
(
Java API
). Line 3
import
java.util.Scanner;
// program uses class Scanner
is an
import
declaration
that helps the compiler locate a class that's used in this program.
It indicates that the program uses the predefined
Scanner
class (discussed shortly) from
the package named
java.util
. The compiler then ensures that you use the class correctly.
Common Programming Error 2.7
All
import
declarations must appear before the first class declaration in the file. Placing
an
import
declaration inside or after a class declaration is a syntax error.
Common Programming Error 2.8
Forgetting to include an
import
declaration for a class that must be imported results in a
compilation error containing a message such as “
cannot
find
symbol
.” When this occurs,
check that you provided the proper
import
declarations and that the names in them are
correct, including proper capitalization.
Software Engineering Observation 2.1
In each new Java version, the APIs typically contain new capabilities that fix bugs, improve
performance or offer better means for accomplishing tasks. The corresponding older versions
are no longer needed and should not be used. Such APIs are said to be
deprecated
and might
be removed from later Java versions.
You'll often encounter deprecated APIs when browsing the online API documentation.
The compiler will warn you when you compile code that uses deprecated APIs. If you compile
your code with
javac
using the command-line argument
-deprecation
, the compiler will
tell you which deprecated features you're using. For each one, the online documentation
feature that replaces the deprecated one.
Line 5
public class
Addition
begins the declaration of class
Addition
. The filename for this
public
class must be
Addition.java
. Remember that the body of each class declaration starts with an opening
left brace (line 6) and ends with a closing right brace (line 27).
The application begins execution with the
main
method (lines 8-26). The left brace
(line 9) marks the beginning of method
main
's body, and the corresponding right brace
(line 26) marks its end. Method
main
is indented one level in the body of class
Addition
,
and the code in the body of
main
is indented another level for readability.
Keyboard
A
variable
is a location in the computer's memory where a value can be stored for use later
in a program. All Java variables
must
be declared with a
name
and a
type
before
they can be
used. A variable's
name
enables the program to access the
value
of the variable in memory. A