Java Reference
In-Depth Information
3>=4||4>=3
true
&&
false
||
true
-3<3||3>-3
true
&& (
false
||
true
)
SR7.
Reread Sec. 1.1.7 on function calls. Remember that, in a function call like
Math.abs(25 * -4)
, the expression
25 * -4
is called the
argument
of the func-
tion call. Have Java evaluate some calls to the functions in class
Math
.
Experiment with expressions that have two or more calls, for example
Math.min(25, 4) + Math.max(25, 4)
.
1.2
Variables, declarations, assignments
In mathematics, a
variable
is a name together with an associated value. In pro-
gramming, we use the same notion of a variable, and we draw a variable as a
named box with the value in the box. Below is an
int
variable called
quantity
whose value is
4
.
Activity
1-4.1
4
quantity
In Java, a variable may be associated with different values at different times, but
the values must all be of the same type. For example,
quantity
may be associ-
ated only with values of type
int
. When it is important to note what the type of
a variable is, we place the type to the right of the box. Below is the same vari-
able,
quantity
, and a second variable
price
, which is of type
double
.
4
price
3.99
quantity
double
int
In Java, a variable must be
declared
before it can be used. Below are Java
declarations
for variables
quantity
and
price
. Note that each declaration con-
sists of three things: a type, the name of the variable, and a semicolon.
int
quantity;
double
price;
In any particular context, a variable can be declared only once. For example,
Variable names, identifiers, and keywords.
A variable name, like
quantity
, is an
identifier
. In
Java, identifiers are writen as a sequence of letters, digits,
$
, and _, but the first
is not a digit. Java programmers do not use
$
and tend not to use
_
except in one
situation, which you will see later. Java programmers follow certain conventions
for identifiers, which we will explain from time to time. They are summarized
in Sec. 13.1.1.
Identifiers are case sensitive:
truth
and
Truth
are different.
Keywords (e.g.
int
and
double
) may not be used as identifiers. To see the
list of keywords, look up
keyword
in the glossary of the ProgramLive CD. In
most IDEs, keywords are shown in their own distinct color, so you should not
have trouble distinguishing keywords from identifiers.
Search WWH ::
Custom Search