Java Reference
In-Depth Information
Java variables
Primitive type
variables
Reference
variables
FIGURE 3-6
Java variables
Primitive type variables store data directly into their own memory spaces. Reference
variables store the address of the object containing the data. An object is an instance of
a
class
and the operator
new
is used to instantiate an object. In some languages, such as
C++, reference variables are called pointers.
Before discussing the
class
String
, we first discuss how to use predefined methods in a
program.
in a Program
Recall that a method is a collection of instructions. When a method executes, it
accomplishes something. The method
main
, which you used in Chapter 2, executes
automatically when you run a Java program. Other methods execute only when they are
activated—that is, called. Java comes with a wealth of classes called predefined classes.
In addition, every predefined
class
contains many predefined methods, which accom-
plish useful results. In this section, you do not learn how to write your own methods, but
you do learn how to use some of the predefined classes and methods that accompany Java.
Recall from Chapter 2 that predefined classes are organized as a collection of packages,
called class libraries. A particular
package
can contain several classes and each
class
can
contain several methods. Therefore, to use a predefined
class
and/or a method, you need
to know the name of the
package
, the name of the
class
, and the name of the method.
To use a method, you also need to know a few other things, which are described shortly.
There are two types of methods in a
class
:
static
and non-
static
.A
static
method can be used, that is, called, using the name of the
class
containing the method.
(Chapter 8 describes these methods in detail. At this point, you need to know only how
to use predefined methods, which may be
static
or non-
static
.)
The Java system contains the
class
Math
, which in turn contains powerful and useful
mathematical functions. The
class
Math
is contained in the
package
java.lang
.
Every method of the
class
Math
is a
static
method. Therefore, you can use every
method of the
class
Math
using the name of the
class
, which is
Math
.
Search WWH ::
Custom Search